C# 如何在 Windows 窗体组合框中捕获回车键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1226726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I capture the enter key in a windows forms combobox
提问by Presidenten
How do I capture the enter key in a windows forms combo box when the combobox is active?
当组合框处于活动状态时,如何在 Windows 窗体组合框中捕获回车键?
I've tried to listen to KeyDown and KeyPress and I've created a subclass and overridden ProcessDialogKey, but nothing seems to work.
我试过听 KeyDown 和 KeyPress,我创建了一个子类并覆盖了 ProcessDialogKey,但似乎没有任何效果。
Any ideas?
有任何想法吗?
/P
/P
采纳答案by Winston Smith
Hook up the KeyPress event to a method like this:
将 KeyPress 事件连接到这样的方法:
protected void myCombo_OnKeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
MessageBox.Show("Enter pressed", "Attention");
}
}
I've tested this in a WinForms application with VS2008 and it works.
我已经在带有 VS2008 的 WinForms 应用程序中对此进行了测试,并且可以正常工作。
If it isn't working for you, please post your code.
如果它不适合您,请发布您的代码。
回答by Petros
or altertatively you can hook up the KeyDown event:
或者,您可以连接 KeyDown 事件:
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter pressed.");
}
}
回答by Josip Medved
In case you define AcceptButton on your form, you cannot listen to Enter key in KeyDown/KeyUp/KeyPress.
如果您在表单上定义了 AcceptButton,您将无法在 KeyDown/KeyUp/KeyPress 中听到 Enter 键。
In order to check for that, you need to override ProcessCmdKey on FORM:
为了检查这一点,您需要覆盖 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);
}
}
In this example that would give you message box if you are on combo box and it works as before for all other controls.
在这个例子中,如果你在组合框上,它会给你消息框,它像以前一样适用于所有其他控件。
回答by zebrabox
It could be that your dialog has a button that's eating the enter key because it's set to be the AcceptButton in the form property.
If that's the case then you solve this like this by unsetting the AcceptButton property when the control gets focus then resetting it back once the control loses focus ( in my code, button1 is the accept button )
可能是您的对话框有一个正在吃回车键的按钮,因为它被设置为表单属性中的 AcceptButton。
如果是这种情况,那么您可以通过在控件获得焦点时取消设置 AcceptButton 属性,然后在控件失去焦点后将其重新设置来解决此问题(在我的代码中, button1 是接受按钮)
private void comboBox1_Enter(object sender, EventArgs e)
{
this.AcceptButton = null;
}
private void comboBox1_Leave(object sender, EventArgs e)
{
this.AcceptButton = button1;
}
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
MessageBox.Show("Hello");
}
}
I have to admit not liking my own solution as it seems a little hacky to unset/set the AcceptButton property so if anyone has a better solution then I'd be interested
我不得不承认不喜欢我自己的解决方案,因为取消/设置 AcceptButton 属性似乎有点麻烦,所以如果有人有更好的解决方案,那么我会感兴趣
回答by jay_t55
private void comboBox1_KeyDown( object sender, EventArgs e )
{
if( e.KeyCode == Keys.Enter )
{
// Do something here...
} else Application.DoEvents();
}
回答by Jay
Try this:
尝试这个:
protected override bool ProcessCmdKey(ref Message msg, Keys k)
{
if (k == Keys.Enter || k == Keys.Return)
{
this.Text = null;
return true;
}
return base.ProcessCmdKey(ref msg, k);
}
回答by user1694951
protected void Form_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13) // or Key.Enter or Key.Return
{
MessageBox.Show("Enter pressed", "KeyPress Event");
}
}
Don't forget to set KeyPreview to true on the form.
不要忘记在表单上将 KeyPreview 设置为 true。