简体   繁体   English

按键关闭表格VB.net

[英]Key Press to Close Form VB.net

So I've been working on this project to autorun on my flash drive, whenever it gets plugged it. 因此,我一直在研究这个项目,以便在插入闪存驱动器时自动运行它。 It just displays my contact info and a little message, so I can get it returned. 它仅显示我的联系信息和一条小消息,因此我可以将其退回。

Because I want people to actually read the message, the close button is disabled. 因为我希望人们实际阅读邮件,所以关闭按钮被禁用。 You have to check a little box, and then hit the "OK" button. 您必须选中一个小方框,然后单击“确定”按钮。

Of course, I don't want to do this whenever I plug in my own flash drive. 当然,我不想在插入自己的闪存驱动器时执行此操作。 I'm trying to make a keyboard shortcut to close it automatically. 我正在尝试使键盘快捷键自动关闭。 I saw this post here but it didn't work for me. 我在这里看到了这篇文章但对我没有用。 Here's the code from that post: 这是该帖子中的代码:

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    If e.Alt AndAlso e.KeyCode = Keys.X Then
Application.Exit()
End If
End Sub

Any help is very appreciated. 任何帮助都非常感谢。 Thanks! 谢谢!

Also, if you think my code is sloppy and want to clean it up, here it is. 另外,如果您认为我的代码草率而想要清理它,就在这里。

Public Class Form1

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    e.Cancel = True
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonOK.Click
    End
End Sub

Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    If CheckBox1.Checked = True Then
        ButtonOK.Enabled = True
    Else
        ButtonOK.Enabled = False
    End If
End Sub

Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    If e.Alt AndAlso e.KeyCode = Keys.X Then
        End
    End If
End Sub
End Class

Firstly, NEVER EVER use End . 首先, 永远不要使用End That is just plain bad under any circumstances. 在任何情况下,这都是很糟糕的。

What you should be doing is testing the Checked value of your CheckBox in the FormClosing event handler and cancelling if and only if it's not checked. 您应该做的是在FormClosing事件处理程序中测试CheckBoxChecked值,并在且仅当未选中时取消它。 Logically, you should then check the CheckBox and call Close on the form when you detect that desired key combination. 从逻辑上讲,当您检测到所需的组合键时,应检查CheckBox并在窗体上调用Close

Actually, I'd probably not call Close either, but rather call PerformClick on the Button . 其实,我可能不叫Close要么,而是调用PerformClick上的Button That way, the key combination is guaranteed to do exactly the same thing as clicking the Button . 这样,可以确保按键组合与单击Button完全相同。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonOK.Click
    Close()
End Sub

Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    ButtonOK.Enabled = CheckBox1.Checked
End Sub

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    If e.Alt AndAlso e.KeyCode = Keys.X Then
        CheckBox1.Checked = True
        ButtonOK.PerformClick()
    End If
End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    e.Cancel = Not CheckBox1.Checked
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM