I think its called e.keys?
How do I make a function so when you press a key like "F2"
I can make it do another function like bring up a message box?
Printable View
I think its called e.keys?
How do I make a function so when you press a key like "F2"
I can make it do another function like bring up a message box?
The 'e' is the second parameter of the relevant event handler, which would likely be for your form's KeyDown event. You would then test the e.KeyCode or e.KeyData property, depending on exactly what behaviour you want with respect to modifier keys, against your target value. If they match then you perform your action. The target value will be a single or composite value from the Keys enumeration, e.g. Keys.F2.
Note that, if you have controls on your form that process keyboard input, e.g. TextBoxes, and you want your form to trap this key(s) no matter what control has focus, you must set the form's KeyPreview to True.
I just want it so when you press F2 it will pop open a message box...
Although already stated....
Step 1 Set the forms keyPreview to true
Step 2 Create a sub on the form to handle the keydown event
Step 3 in the Sub you created in step 2 check if e.KeyCode = Keys.F2 and if so call MessageBox.Show("F2 pressed")
Specifying 7777's thing.
vb Code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown If e.KeyCode = Keys.F2 Then MessageBox.Show("Example.") End If End Sub
Do I have to like, Declare something? It gives me an error.
are you using VB 2005, 2008 or 2010 and not vb6? make sure to not put that code in a sub but right below the "Public Class ..." line
Oh ok, I was putting it in the wrong class, Thank you, Works great. +rep to all.