简体   繁体   English

KeyDown 未拾取“返回”键 C#

[英]KeyDown not picking up 'Return' key C#

    private void idTextEdit_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Return)
        {
            e.Handled = true;
            SearchButtonClick(sender, EventArgs.Empty);
        }
    }

I have a text box where this code check fires for every single keypress except for the Enter/Return key which for some reason does nothing.我有一个文本框,除了 Enter/Return 键之外,每次按键都会触发此代码检查,该键由于某种原因什么都不做。 The cursor that is active on the textbox disappears, so I'm thinking it changes focus before the keydown event can fire but I'm not sure.在文本框上处于活动状态的 cursor 消失了,所以我认为它会在 keydown 事件触发之前改变焦点,但我不确定。 How can I get the return key to stop deselecting the box and register as a keypress.如何获得返回键以停止取消选择框并注册为按键。 Also there is no other code that would set the enter key to have a different functionality and it's a pretty simple one text box screen for testing.此外,没有其他代码可以将回车键设置为具有不同的功能,这是一个非常简单的用于测试的文本框屏幕。

If you have an AcceptButton set then you'll get the behaviour you are seeing.如果您设置了AcceptButton ,那么您将获得您所看到的行为。

I tried your code with my sample form and it worked as expected.我用我的示例表单尝试了您的代码,它按预期工作。

I then set the AcceptButton to one of the buttons and the text box stopped responding to the Enter .然后我将AcceptButton设置为其中一个按钮,并且文本框停止响应Enter Setting AcceptsReturn on the text box had no effect.在文本框上设置AcceptsReturn无效。

Set the AcceptsEnter property to trueAcceptsEnter属性设置为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM