Win*_*ith 19
将KeyPress事件连接到这样的方法:
protected void myCombo_OnKeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
MessageBox.Show("Enter pressed", "Attention");
}
}
Run Code Online (Sandbox Code Playgroud)
我已经在VS2008的WinForms应用程序中对此进行了测试,但它确实有效.
如果它不适合您,请发布您的代码.
Jos*_*ved 17
如果您在表单上定义AcceptButton,则无法在KeyDown/KeyUp/KeyPress中侦听Enter键.
为了检查它,您需要覆盖FORM上的ProcessCmdKey:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if ((this.ActiveControl == myComboBox) && (keyData == Keys.Return)) {
MessageBox.Show("Combo Enter");
return true;
} else {
return base.ProcessCmdKey(ref msg, keyData);
}
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,如果您在组合框中,它将为您提供消息框,并且它像以前一样用于所有其他控件.
或者您可以将KeyDown事件挂钩:
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter pressed.");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
38814 次 |
| 最近记录: |