Question Question about a keydown event

thirteentwenty

Well-known member
Joined
Oct 9, 2008
Messages
80
Location
Honolulu
Programming Experience
Beginner
I've been using this bit of code to handle keydown events for different controls (in this case a textbox) that need them

VB.NET:
Private Sub TextBox1_KeyDown(ByVal eventsender As System.Object, ByVal eventargs As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    Dim KeyCode As Short = eventargs.KeyCode
    ' Dim Shift As Short = eventargs.KeyData \ &H10000 ' figure this out later
    If KeyCode = 13 Then
        ' Do something
    End If
End Sub

I've got a couple of questions about it....
  1. Is this a good way to do this, or is there a better way to do this
  2. what does the below line (in code it has been commented out) mean/do?
VB.NET:
Dim Shift As Short = eventargs.KeyData \ &H10000 ' figure this out later

* note: this is from a test project that I keep handy to experiment with so please don't mind the naming convention...
 
KeyCode type is Keys, not Short.
VB.NET:
If e.KeyCode = Keys.Enter Then
The KeyData type is also Keys, this is a flags enumeration which allows bitwise operations of the member values, like combining them to a single Keys value.

&H10000 is the integer value of Keys.Shift value, so I think this integer division was supposed to remove the Shift value from the KeyData combination. The correct operation would be this:
VB.NET:
Dim k As Keys = e.KeyData And Not Keys.Shift
 
Thank you for the move, and the explanation... I always wondered why it was that when I used vs to create the sub it didnt work... and that silly thing (&H100000) has nothing to do with anything in there... wonderful!!!

now all i have to do is go in and change every single one that i used that bit of code in YAY!!!

anyways, does this look better?

VB.NET:
    Private Sub control_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt_payments_invoiceNumber.KeyDown, dtp_payments_paymentDate.KeyDown, txt_payments_description.KeyDown
        Dim keycode As Keys = e.KeyCode
        If keycode = Keys.Enter Then
            GetNextControl(ActiveControl, True).Select()
        End If
    End Sub
 
Back
Top