Keydown event

Rhayezelle

Member
Joined
Aug 14, 2010
Messages
24
Location
Philippines
Programming Experience
Beginner
I wonder what's wrong with my code..


: this code works
Private Sub TxtLname_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtLname.KeyDown

If e.KeyCode = Keys.Enter Then
If IsNumeric(TxtLname.Text) Then
MsgBox("Invalid Input")
End If
End If
End Sub

: when I replace the keys.enter with "keys.tab" it won't work.. please help me
Private Sub TxtLname_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtLname.KeyDown

If e.KeyCode = Keys.tab Then
If IsNumeric(TxtLname.Text) Then
MsgBox("Invalid Input")
End If
End If
End Sub
 
Rhayezelle, I think the biggest question here is, what are you trying to achieve? Are youe trying to validate the user's input?
 
JuggaloBrotha makes a good point, what are you attempting to accomplish?


But I can still shed some light onto your problem. First check out here:
Control.KeyDown Event (System.Windows.Forms)

Quoted from that MSDN page for the Control.KeyDown event:

Certain keys, such as the TAB, RETURN, ESCAPE, and arrow keys are handled by controls automatically. To have these keys raise the KeyDown event, you must override the IsInputKey method in each control on your form. The code for the override of the IsInputKey would need to determine if one of the special keys is pressed and return a value of true.

What this basically is saying is that the Control itself has captured the TAB KeyDown event and processed it and already reacted (tab changes focus between controls). You can override the IsInputKey method as the quotation says... but this probably isn't the right time to be doing this.

There is other events on the Control object that may be a better entry, which is why JuggaloBrotha is asking what it is you're attempting to do. We may be able to point you at a better tool for the job. For instance if it's validation you're attempting to do, you may want to try the validating event, or maybe something else in the focus/validating event chain.

see here:

Control.Validating Event (System.Windows.Forms)

note the focus/validation event chain:

When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:

Enter

GotFocus

Leave

Validating

Validated

LostFocus

When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:

Enter

GotFocus

LostFocus

Leave

Validating

Validated



You may also want to bookmark the MSDN page and read through it when ever you have a problem. Whenever I write some code that starts acting completely against the way I "assumed" it would work, I go to MSDN... there they tell me how it actually works. Best place to always start is go to the page describing the main class you're playing with... in this case it was the Control class, and the KeyDown event.

If you don't know the class, just search near by what you think it might be. Just searching KeyDown brings up Control.KeyDown event. Or you may know it's related to the System.Windows.Forms namespace.
 
Back
Top