按下 Escape 键时关闭 VB.NET 窗体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13509065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:27:13  来源:igfitidea点击:

Closing a VB.NET form when Escape key is pressed

vb.netvisual-studio-express

提问by whytheq

I'm using VB 2010 Express.

我正在使用 VB 2010 Express。

In C#I would set the forms CancelButtonproperty.

C#我将设置表单CancelButton属性。

For this VB form I don't have a CancelButton so I suspect I need to program either KeyPressor KeyDown.

对于这个 VB 表单,我没有 CancelButton,所以我怀疑我需要编写KeyPressKeyDown.

  1. What is the difference between these two events?
  2. Which should I use?
  3. I assume the general code for this is as follows?:

    If e.KeyCode = Keys.Escape Then
        Close()
    End If
    
  1. 这两个事件有什么区别?
  2. 我应该使用哪个?
  3. 我假设通用代码如下?:

    If e.KeyCode = Keys.Escape Then
        Close()
    End If
    

I have certain .Focuscode within other controls of the form then it becomes pointless putting this in the main forms event procedure as the main form never really hasthe focus.

.Focus在表单的其他控件中有某些代码,然后将它放在主表单事件过程中变得毫无意义,因为主表单从未真正拥有焦点。

回答by patrick choi

Set your form keydown to

将您的表单 keydown 设置为

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    If e.KeyCode = Keys.Escape Then Me.Close()
End Sub

Then, Remember to set the KeyPreview property on the form to TRUE.

然后,记住将表单上的 KeyPreview 属性设置为 TRUE。

回答by Frank Lindahl

  1. You say "the VB form has no Cancel Button" -- so give it an invisible one.a.) Add the button to the form. b.) Set it's Visibleproperty to False. (Remember, computers don't make mistakes -- they lie.So let's get even.)
  2. On the form properties menu, set the CancelButtonProperty to your invisible cancel button.
  3. On the form properties menu, set the KeyPreviewProperty to True.
  4. Double click on your invisible CancelButton and add Me.Dispose()
  5. Now you have a Micro-Nasty mess to deal with. One of the basic principles of structured programming is that "there must be only one exit from a routine." You don't want to handle an exit routine in two different places. And your user can get out by either using "Esc" in the upper right hand corner. The heck with structured programming. So . . .
  6. The upper right black "X" on red button calls FormName_Deactivate -- if the name of your form is FormSanta then it is FormSanta_Deactivate. For simplicity's sake, never mind the right way, (Micro-serfs in your velvet sweat-shop, can you hear me crying?) give your upper right "X" button and your invisible exit button the same code. Then add on Me.Close()
  1. 你说“VB 表单没有取消按钮”——所以给它一个不可见的按钮a.) 将按钮添加到表单中。b.) 将其Visible属性设置为False。(请记住,计算机不会犯错——它们会说谎。所以让我们平起平坐。)
  2. 在表单属性菜单上,将CancelButton属性设置为不可见的取消按钮
  3. 在表单属性菜单上,将KeyPreview属性设置为True
  4. 双击你不可见的 CancelButton 并添加 Me.Dispose()
  5. 现在你有一个微讨厌的烂摊子要处理。结构化编程的基本原则之一是“例程必须只有一个出口”。您不想在两个不同的地方处理退出例程。您的用户可以通过使用右上角的“Esc”退出。结构化编程的鬼斧神工。所以 。. .
  6. 红色按钮上的右上角黑色“X”调用 FormName_Deactivate -- 如果您的表单名称是 FormSanta 那么它就是 FormSanta_Deactivate。为简单起见,不要介意正确的方法,(你天鹅绒血汗工厂里的微型农奴,你能听到我在哭吗?)给你右上角的“X”按钮和你的隐形退出按钮相同的代码。然后添加 Me.Close()

回答by My Brain

My solution is also in the form properties:

我的解决方案也是表单属性:

  • set the CancelButton Property to button that performs your cancel function
  • 将 CancelButton 属性设置为执行取消功能的按钮

回答by Kevin Henzel

"KeyPreview" Property of the form has to be set to true, or this will not work...

表单的“KeyPreview”属性必须设置为true,否则这将不起作用...