OK-取消对话框:处理"Enter"键的按下 [英] OK-Cancel Dialog: handling the 'Enter' key press

查看:53
本文介绍了OK-取消对话框:处理"Enter"键的按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Winforms中构建一个 Dialog .创建它时,它具有两个确定"和取消"按钮,这正是我想要的.在此对话框中,我还有一个 TextBox 和一个 Sub (在VB.NET中进行编码),用于处理其 KeyPress 事件.按下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"按钮,返回结果并关闭.在设计器和运行应用程序的实际表单中,确定"按钮都是突出显示的,这意味着它始终具有某种 focus (因为缺少更好的术语).如何禁用对话框的此功能?当我调试代码时,按Enter甚至不会进入处理 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属性设置为确定"按钮.它带有粗线边框,指示它是默认按钮,当用户按下Enter时,其Click事件将自动运行.如果将按钮的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-取消对话框:处理"Enter"键的按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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