KeyDown 没有拿起“返回"键 C# [英] KeyDown not picking up 'Return' key C#

查看:23
本文介绍了KeyDown 没有拿起“返回"键 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我有一个文本框,其中每个按键都会触发此代码检查,除了 Enter/Return 键由于某种原因什么都不做.文本框上活动的光标消失了,所以我认为它会在 keydown 事件触发之前改变焦点,但我不确定.如何获得返回键以停止取消选择框并注册为按键.此外,没有其他代码可以将回车键设置为具有不同的功能,而且它是一个非常简单的文本框屏幕,用于测试.

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. 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. 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.

推荐答案

如果您设置了 AcceptButton,那么您将获得您所看到的行为.

If you have an AcceptButton set then you'll get the behaviour you are seeing.

我用我的示例表单尝试了你的代码,它按预期工作.

I tried your code with my sample form and it worked as expected.

然后我将 AcceptButton 设置为按钮之一,文本框停止响应 Enter.在文本框中设置 AcceptsReturn 没有效果.

I then set the AcceptButton to one of the buttons and the text box stopped responding to the Enter. Setting AcceptsReturn on the text box had no effect.

这篇关于KeyDown 没有拿起“返回"键 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆