-2

I'm currently writing code for a project in VB.NET where if you hit the "escape" button you get a messagebox saying "This exits the program, would you like to leave? 'Y' or 'N'". What code would I write to make it so that if you hit "Y" the program closes?

This is what I have so far:

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = Keys.Escape Then
        MsgBox("This button exits the program. Do you want to exit the program?")
        If Button = e.KeyCode = Keys.Y Then
            End
        End If
    End If
End Sub
Bugs
  • 4,491
  • 9
  • 32
  • 41
Seth03215
  • 3
  • 1
  • 6
  • `Application.Exit`, but I'm not adding an answer as this question [is a duplicate](http://stackoverflow.com/questions/7146080/closing-applications) (different programming language, but the same framework). – AStopher Jan 05 '17 at 23:17
  • Use [MessageBox.Show](https://msdn.microsoft.com/en-us/library/ba3x8zfh(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2) – Bugs Jan 05 '17 at 23:24
  • @cybermonkey, I do appreciate the answer but unfortunately it did not work when I tested it, it must be something in my code. – Seth03215 Jan 05 '17 at 23:36
  • 1
    You must design a custom message box-like form in order to do that. When Windows's message box is shown it'll both steal focus but also trap the rest of the application in a modal loop. The latter means that the rest of the application will be unable to operate and respond to events (such as `KeyDown`). – Visual Vincent Jan 05 '17 at 23:37
  • In addition to @Jinx88909's comment you should use `MessageBox.Show()` since that's the native .NET way to do it. `MsgBox()` is a VB implementation in VB6 and older, which currently exists only for backwards compatibility. – Visual Vincent Jan 05 '17 at 23:45
  • @VisualVincent, thank you very much – Seth03215 Jan 08 '17 at 21:54

3 Answers3

2

This will give you what you are after:

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = Keys.Escape Then
        If MessageBox.Show("message", "caption", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            Close()
        End If
    End If
End Sub

Screenshot showing how it looks when I press the Esc button:

enter image description here

Bugs
  • 4,491
  • 9
  • 32
  • 41
-1

Try This One

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    If e.KeyCode = Keys.Escape Then
        Dim ask As MsgBoxResult
        ask = MsgBox("Close Program ?", MsgBoxStyle.YesNo, "Exit")
        If ask = MsgBoxResult.Yes Then
            Me.Close()
        End If
    End If
End Sub
Bugs
  • 4,491
  • 9
  • 32
  • 41
Ali Ahmad
  • 81
  • 2
  • 9
  • I didn't DV however I did suggest an edit which fixes obvious errors. You could however replace lines 3 to 5 with `If MessageBox.Show("message", "caption", MessageBoxButtons.YesNo) = DialogResult.Yes Then` – Bugs Jan 06 '17 at 12:14
  • 1
    @Jinx88909 : I reviewed your edit and it looked fine. Two more need to approve it before it's officially approved. – Visual Vincent Jan 06 '17 at 12:44
  • Thanks @VisualVincent appreciate it. – Bugs Jan 06 '17 at 12:45
  • I Have Checked this code and there is no single problem with this code – Ali Ahmad Jan 09 '17 at 03:46
  • @AliAhmad `dim ask as MsgBoxResult()` was causing an issue. This needed to be changed to `Dim ask As MsgBoxResult`. Note the removing of `()` and you had missed off the `End If` for `If ask`. Apart from that the code was fine. Like I said I didn't DV. Unless the answer is heavily wrong I don't DV. – Bugs Jan 09 '17 at 13:00
-1
  Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

  Select Case e.KeyCode

       Case Keys.Escape

            Msgbox("Close",vbInformation)
            Me.Close()

   End Select

  End Sub

Hope This Helps as a guide.

Troy Reyes
  • 42
  • 4
  • I didn't DV but I think the OP would like to ask a question with a _Yes/No_ option and not just show `Msgbox("Close",vbInformation)` – Bugs Jan 06 '17 at 12:07