Question Escape Key close form

Status
Not open for further replies.

thirteentwenty

Well-known member
Joined
Oct 9, 2008
Messages
80
Location
Honolulu
Programming Experience
Beginner
I'm not sure if I'm posting this in the correct area, please move if need be...

OK, I've got this little bit of code that is _supposed_ to close the form its set in. Code to follow

It works OK on most forms, but when I put it into a form called frm_about it fails...

Mind you this is not a preselected "About Box", it's just a regular windows form that I've named frm_about...

any ideas?

VB.NET:
Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Select Case keyData
            Case Keys.Escape
                Me.Close()
        End Select
        Return MyBase.ProcessDialogKey(keyData)
    End Function

Thanks for the assist...
 
Alternative Solution

You can do as follows too...

Add event handler to controls to your form. (frm_about in this case)

AddHandler textbox1.KeyPress, AddressOf FormLevelKeyHandle
AddHandler textbox2.KeyPress, AddressOf FormLevelKeyHandle
AddHandler button1.KeyPress, AddressOf FormLevelKeyHandle



And add this function which is get called when a key is pressed.

Private Sub FormLevelKeyHandle(ByVal o As [Object], ByVal e As KeyPressEventArgs)
If Asc(e.KeyChar) = Keys.Escape Then
Me.Dispose()
End If
End Sub
 
Last edited:
Thanks meparashar... I'll give it a shot when I get to to work on Tuesday...

But there's been a new development, it's not just about page, it's anything with flash on it...
 
Sorry for bringing this back up, but I had to move on with the project... I'm sure most people knows what that's like...

anyways, I'm back to this bit of code as I need to set keypress events for a few other things,

hit the escape key to close the window seems like it should be the easiest and from there I should (with any luck) be able to figure the others out...

So right now, I'm doing as meparashar suggested and am using the addhandler bit... here is my code

VB.NET:
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles MyBase.KeyPress

        AddHandler Me.KeyPress, AddressOf Form1_KeyPress
        MessageBox.Show("You pressed " & e.KeyChar.ToString)

End Sub

but when I do this nothing happens...no messagebox nada...

any suggestions?

and as always thank you to whom ever helps out with this... I can only imagine what its like to work with noobs like me =(
 
The event handler you have shown is handling the KeyPress event for MyBase so it never gets called. Handle Me.Keypress instead. Then there's no need to use AddHandler. which you shouldn't use for static controls only dynamically created controls. Best practice is to use the Handles keyword for static controls, use the AddHandler keyword for dynamically created objects.

As for closing the form with the press of the escape key; I would think you would only want to do this for dialogs. In this case it can be done with no code if you have a Cancel button. Set the Cancel button's DialogResult property to Cancel, set the Form's CancelButton property to the Cancel button. In case you have a dialog that has only an OK button and you want to close the form with the escape key, set the OK button's DialogResult property to OK and the Form's AcceptButton property to the OK button.

Here's code for closing the form with the escape key:
VB.NET:
Private Sub Form1_KeyUp(ByVal sender As Object, _
  ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    If e.KeyCode = Keys.Escape Then
        Me.Close()
    End If
End Sub
Lookup the Control.IsInputKey Method to understand why the KeyPress and KeyDown events won't work without more code.
 
Hey Paszt,

Thanks for the info and the link, I'll be reading up on this more... the escape key to close a form thing is something that I've been using so that I can close the forms out quickly while debugging, although I was going to keep it in the "about" form, which is the one specifically that I'm having issues with

In the link that you provided it calls protected overrides function... which is what I was using as a generic copy/paste code (see original post). But for some reason, it didn't work on the one form that I was going to leave it in... frustrating...

Thanks again!
 
Hey guys,

Thanks for the help, it turns out that everything worked out in the end... Apparently I have been placing the code in the wrong area... the trout slapping may begin!!

anyways, I ended up using Paszts (code to follow) method as its easily transferable from form to form (like i said I'm using this as a generic thing so I can close out of the applications quickly during debugging...

VB.NET:
Option Strict Off
Option Explicit On

Public Class Form1

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

End Class

Thanks again guys...
 
Hey guys,

Thanks for the help, it turns out that everything worked out in the end... Apparently I have been placing the code in the wrong area... the trout slapping may begin!!

anyways, I ended up using Paszts (code to follow) method as its easily transferable from form to form (like i said I'm using this as a generic thing so I can close out of the applications quickly during debugging...

VB.NET:
Option Strict Off
Option Explicit On

Public Class Form1

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

End Class

Thanks again guys...
I would recommend changing that 'Option Strict Off' line to 'Option Strict On'
 
I would recommend changing that 'Option Strict Off' line to 'Option Strict On'

I'm not sure exactly what that does but I'll do it =), I saw on a forum post else where that the combination of Option Strict Off and Option Explicit On was preferred (may just for them) I'm still very new with this and really just learning on the fly... there's no need to respond, here I'll just post another thread asking about it if I can find a clear (and in laymen terms) what those options are...

thanks again for all the help
 
Status
Not open for further replies.
Back
Top