按键预览和接受按钮
使用 winforms,我已将 KeyPreview 属性设置为 true,并为基本表单中的正确按键事件提供了事件句柄。
在从它继承的表单中,我根据应用程序的要求设置了 AcceptButton 属性。
在某些情况下,我希望 Enter 键具有与 AcceptButton 不同的功能。
我希望捕获基本表单中的回车键按下情况,并检查我不希望触发 AcceptButton 事件的特殊情况。
不过,似乎 AcceptButton 单击是在我的 basef 表单中的任何关键事件之前触发的。 我可以将功能写入可能的接受按钮的单击事件中,但是,在我看来,这将是一种黑客行为。
有什么建议么?
谢谢。
Using winforms, I have set the KeyPreview property to true and have event handles for the proper key events within the base form as well.
Within the forms that inherit from it, I set the AcceptButton property based on the requirements of the application.
There are certain cases in which I want the enter key to have functionality different than that of the AcceptButton.
I was hoping to capture the enter key press within my base form and check for the special cases where I do not want the AcceptButton event to fire.
It appears though, that the AcceptButton click is fired before any of the key events within my basef form. I could write functionality into the click events of the possible acceptbuttons, but, in my opinion, that would be a hack.
Any suggestions?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
处理此问题的另一种方法是重写表单的 ProcessDialogKey() 方法,您可以在其中抑制接受和/或取消按钮。 例如,我有一个带有过滤器编辑器的应用程序,可以根据用户输入过滤网格。 我希望当过滤器编辑器控件获得焦点来应用过滤器时,用户能够按回车键。 问题是接受按钮代码运行并关闭表单。 下面的代码解决了这个问题。
您可以通过将以下代码放入基本对话框表单中来更进一步。 然后,您可以根据需要抑制子类中控件的接受按钮。
Another way to handle this is to override the form's ProcessDialogKey() method where you can suppress the accept and/or cancel buttons. For example, I have an application with a filter editor that filters a grid based on user input. I want the user to be able to hit the return key when the filter editor control has the focus to apply the filter. The problem is the accept button code runs and closes the form. The code below resolves the issue.
You can take this even further by dropping the following code in a base dialog form. Then you can suppress the accept button for controls in subclasses as necessary.
作为我们的解决方法,我们捕获了我们想要覆盖接受按钮功能的控件的进入和离开事件。 在 Enter 事件中,我们将当前接受按钮保存在私有变量中,并将接受按钮设置为 null。 休假时,我们会将接受按钮重新分配回我们持有的私有变量。
KeyPreview 事件可以执行与上面类似的操作。 如果有人有更优雅的解决方案,我仍然很想知道。
谢谢。
As our workaround, we captured the enter and leave event for the control that we wanted to have override the acceptbutton functionality. Inside the enter event, we held the current accept button in a private variable and set the acceptbutton to null. On leave, we would reassign the acceptbutton back to the private variable we were holding.
The KeyPreview events could have done something similar to the above. If anyone has a more elegant solution, I would still love to know.
Thanks.