C#:Form.AcceptButton 出现问题
我有一个带有按钮的表单,该按钮设置为表单的 AcceptButton。该表单还有其他几个控件。现在,当我在其他控件上按 Enter 时,由于表单上的接受按钮,表单会关闭。取消按钮也是如此。我该如何处理这个问题。我尝试连接表单和控件的按键 keydown 事件。没有一个有效。有解决这个问题的方法吗?
非常感谢, 达特
I have a form with an button which is set as the AcceptButton of the form. The form has several other controls. Now when I press Enter on other controls the form gets closed because of the accept button on the form. Same goes for CancelButton. How do I handle this. I tried hooking on to keypress keydown event of the form and controls. None works. Any work around for this?
Thanks a ton,
Datte
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这就是
AcceptButton
属性的工作原理。它指定每当您按
时自动单击的按钮。如果您不希望出现此行为,请不要将其设置为
AcceptButton
。没有其他理由这样做。That is how the
AcceptButton
property works. It specifies the button that is automatically clicked whenever you press<Enter>
.If you don't want this behaviour, don't set it as the
AcceptButton
. There is no other reason to do it.不太确定您期望表单如何运行,但是您可以执行类似以下操作来对事物有更多的控制:
Not exactly sure about how you expect your form to function, but you could do something like the following to have a little more control over things:
您可以从表单中删除 AcceptButton,并在表单上设置处理其 KeyDown 事件的 KeyPreview 属性。您可以在此处检查 Enter 键并采取相应的操作。
You can remove AcceptButton from form and set the KeyPreview property on the form that'll handle its KeyDown event. There you can check for the Enter key and take the action accordingly.
这是表单的功能之一,即
如果按钮没有焦点,如果您仍然希望在用户单击 Enter 时执行所需的代码...
设置表单的 AcceptButton 属性以允许用户通过按即使按钮没有焦点,也请输入 ENTER。
问候。
This is one of the feature of the form i.e.
if button does not have a focus if you still want desired code to be executed when user click Enter...
Set the AcceptButton property of a form to allow users to click a button by pressing the ENTER even if the button does not have focus.
Regards.
在 VB>net 中试试这个
Try This One In VB>net