title: Key Press to Close Form VB.net
body: <p>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. </p>
  
  <p>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.</p>
  
  <p>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 <a href="https://stackoverflow.com/questions/8111033/close-program-when-the-user-presses-altx">here</a> but it didn't work for me. Here's the code from that post:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Any help is very appreciated. Thanks!</p>
  
  <p>Also, if you think my code is sloppy and want to clean it up, here it is. </p>
  
  <pre><code>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
  </code></pre>