ComboBox接受回车事件的两种方法:
第一种:可以override窗体Form的ProcessDialogKey来抓回车
//拷贝到Form类中。
protected override bool ProcessDialogKey(System.Windows.Forms.Keys aKey)
{
if (aKey == Keys.Enter)
{
if (this.comboBox1.Focused)
{
MessageBox.Show( "我抓到了回车! ");
return true;
}
}
base.ProcessDialogKey(aKey);
return false;
}
第二种,给ComboBox添加KeyUp或KeyDown事件
private void comboBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode==System.Windows.Forms.Keys.Enter)
{
MessageBox.Show( "我抓到了回车! ");
comboBox1.Items.AddRange(new object[]{comboBox1.Text});
}
}