guyz i need your help. I want that everytime I press the ESC "ESCAPE key" there will be a message box popping out then will ask if I want to exit? then if I click the Yes button my Progrma Ends. I also used the form_closing event heres my code. .

Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If MsgBox("Are you sure you want to exit?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Exit") = MsgBoxResult.Yes Then
            End
        Else
            e.Cancel = True
        End If
-------------------------------------------------------------------------------------
Private Sub frmMain_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If Keys.Escape Then
            frmMain_FormClosing(sender, e)
        Else
        End If

    End Sub

hope you help me here...

Recommended Answers

All 6 Replies

Hi,

Change this part of code:

Private Sub frmMain_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If Keys.Escape Then
            Me.Close()
        Else
        End If
    End Sub

I've already tried that and here's my code:

Private Sub frmMain_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If Keys.Escape Then
            If MsgBox("Are you sure you want to exit?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Exit") = MsgBoxResult.Yes Then
                End
            Else
            End If
        End If

but it seems that there is something wrong, it will only run once then afterwards it doesnt.. like if I click the button "No" when the message box appeared and then I press the ESC again, the command does not executed anymore... help me..

Set KeyPreview=True Form property and then handle keypress event.

Private Sub frmMain_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If e.KeyChar = Chr(27) Then
              MsgBox("Place your message...")
        End If
    End Sub

Hi,

Why you changed everything?
Here's how it should look like and after testing works like it should.

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If MsgBox("Are you sure you want to exit?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Exit") = MsgBoxResult.Yes Then
        Else
            e.Cancel = True
        End If

    End Sub

    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If Keys.Escape Then
            Me.Close()
        Else
        End If
    End Sub

thanx.. it really worked! just a follow up question.. how will I know the designated values of each keys in the keyboard? is there anyway to check it like when I press the keyboard a message box will appear then tells the value of what I pressed?here's my code..

Private Sub frmMain_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
MsgBox(e.KeyChar)
end sub

I already set the keypreview to TRUE.. but in only displays the specific character that I press not its value...

Hi,

I think you should start a New thread, because a few members will look into a solved thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.