OK-Cancel Dialog: handling the #39;Enter#39; key press(OK-Cancel Dialog:处理“Enter键按下)
问题描述
我正在 Winforms 中构建一个 Dialog
.当您创建它时,它有两个确定和取消按钮,这就是我想要的.在这个对话框中,我还有一个处理其 KeyPress
事件的 TextBox
和一个 Sub
(在 VB.NET 中编码).当按下Enter"键时,我需要一些事情发生.
I'm building a Dialog
in Winforms. It's got the two OK and Cancel buttons that are there when you create it, which is what I want. In this dialog I also have a TextBox
and a Sub
(coding in VB.NET) that handles its KeyPress
event. I need stuff to happen when the 'Enter' key is pressed.
现在,我已经一次又一次地完成了这样的 KeyPress
处理.但是,这种情况有所不同,因为只要按下Enter"键,对话框就会自动假定您正在按下确定"按钮,返回结果并关闭.在设计器和运行应用程序时的实际表单中,OK 按钮都是突出显示的,这意味着它始终具有某种焦点(因为没有更好的术语).如何禁用对话框的此功能?当我调试我的代码时,按回车键甚至没有到达处理 KeyPress
事件的子程序.它只是关闭对话框并返回结果,因此我无法真正单步执行代码并查看幕后发生的情况.
Now, I've done such KeyPress
handling times and times again. This situation, however, is different, because as soon as 'Enter' key is pressed, the dialog automatically assumes you're pressing the 'OK' button, returns a result and closes. In both the Designer and the actual form when running the application, the OK button is highlight, which means is has some kind of a focus (for the lack of a better term) at all times. How can I disable this feature of a dialog? When I debug my code, pressing enter does not even get to the sub handling the KeyPress
event. It simply closes the dialog and returns the result, therefore I can't really step through the code and see what happens behind the scenes.
要重申这个问题,我该如何禁用此功能?
To restate the question, how can I disable this functionality?
干杯!= )
推荐答案
这是它应该的工作方式.您应该将表单的 AcceptButton 属性设置为 OK 按钮.它获取粗边框以指示它是默认按钮,其 Click 事件在用户按 Enter 时自动运行.如果您将按钮的 DialogResult 属性设置为 OK,那么它还会自动分配 DialogResult 属性以便对话框关闭.
This is the way it is supposed to work. You should set the form's AcceptButton property to the OK button. It gets the fat border to indicate that it is the default button whose Click event automatically runs when the user presses Enter. If you set the button's DialogResult property to OK then it also assigns the DialogResult property automatically so that the dialog closes.
不要修复它.如果要调试它,请在按钮的 Click 事件处理程序上设置中断.如果您想阻止它关闭,请重置按钮的 DialogResult 属性或将表单的 DialogResult 属性设置回 None.永远不要使用 KeyPressed 事件.
Don't fix it. If you want to debug it then set a break on the button's Click event handler. If you want to prevent it from closing then either reset the button's DialogResult property or set the form's DialogResult property back to None. Never use the KeyPressed event.
这篇关于OK-Cancel Dialog:处理“Enter"键按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:OK-Cancel Dialog:处理“Enter"键按下
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01