Question Keys.Tab works but depends on another controls entity

Socarsky

Well-known member
Joined
Dec 27, 2012
Messages
173
Location
Jakarta/Indonesia
Programming Experience
Beginner
There is an small issue in the part of Keys.Tab following code. If There is no control if a from then Keys.
Tab works well but if a form has some controls on it then Keys.Tab does work when I press it.
Do you know a solution of that small issue?

Private Sub txtStockCode_KeyDown(sender As Object, e As KeyEventArgs) Handles txtStockCode.KeyDown
If e.KeyValue = Keys.Enter OrElse _
            e.KeyValue = Keys.F2 OrElse _
            e.KeyValue = Keys.Tab OrElse _
            e.KeyValue = Keys.Down Then
            MessageBox.Show("yay")
End If
 
Unfortunately I could not got a good result in usage of your code. Probably I made it incorrect. The textBox uses autocomplete property and if I press tab then key choose at the top of the list of autocomplete. This is difference of your code I think
Here is your code
VB.NET:
[COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] TextBox1_PreviewKeyDown([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] [COLOR=blue]Object[/COLOR], _
                                    [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] [COLOR=#2B91AF]PreviewKeyDownEventArgs[/COLOR]) [COLOR=blue]Handles[/COLOR] TextBox1.PreviewKeyDown
    [COLOR=blue]If[/COLOR] e.KeyData = [COLOR=#2B91AF]Keys[/COLOR].Tab [COLOR=blue]Then[/COLOR]
        e.IsInputKey = [COLOR=blue]True[/COLOR]
    [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]

[COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] TextBox1_KeyDown([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] [COLOR=blue]Object[/COLOR], _
                             [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] [COLOR=#2B91AF]KeyEventArgs[/COLOR]) [COLOR=blue]Handles[/COLOR] TextBox1.KeyDown
    [COLOR=blue]If[/COLOR] e.KeyData = [COLOR=#2B91AF]Keys[/COLOR].Tab [COLOR=blue]Then[/COLOR]
        [COLOR=blue]Me[/COLOR].TextBox1.SelectedText = [COLOR=#A31515]"    "[/COLOR]
    [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]
 
Back
Top