HaPhan HaPhan - 4 months ago 11
Vb.net Question

Setting tab stop between two panels

I have two panels, each has several textboxes. What I want is something that seems very simple: user enter a textbox in this panel, then press Tab to jump to its 'linked' textbox in the other panel.

But, vb refuses to jump to other panel unless it finishes through all of the textboxes inside one panel, no matter what TapStop it is.

I tried to catch the tab key from a textbox, and send focus to the linked one without success: pressing Tab doesn't even fire the KeyDown nor KeyPress event.

I tried to set TabStop to the panels first, but that also fail.

So, the problem remain.. how to set tabstop.. or any similar means, to the textboxes between two panel so that when user press Tab in one panel, it will switch to the other one ?

I need the Tab key, not anyother key.

Answer

You'll have to set the TabStop property of each control on the form to False and then handle the tabbing yourself, which you can do like this:

Private Sub TextBoxes_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles TextBox6.PreviewKeyDown,
                                                                                             TextBox5.PreviewKeyDown,
                                                                                             TextBox4.PreviewKeyDown,
                                                                                             TextBox3.PreviewKeyDown,
                                                                                             TextBox2.PreviewKeyDown,
                                                                                             TextBox1.PreviewKeyDown
    If e.KeyCode = Keys.Tab Then
        Dim controls As Control() = {TextBox1, TextBox4, TextBox2, TextBox5, TextBox3, TextBox6, Button2}
        Dim currentControlIndex = Array.IndexOf(controls, ActiveControl)
        Dim nextControl = controls(currentControlIndex + 1)

        nextControl.Select()
    End If
End Sub

Every control you want to be able to Tab from must be in the Handles clause and every control that you want to be able to Tab to or from must be in the array and in the order you want to Tab to them in. You should also repeat the first control at the end of the array again, in order to wrap back to the beginning from the end.

Also note that no control will be selected by default if none of them a Tab stops, in which case you must manually Select the control you want to have focus by default in the form's Shown event handler.

EDIT: Here is a more complete example:

Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    'Manually focus the first control after the form is displayed.
    Button1.Select()
End Sub

'Include all the controls that you want to behave as Tab stops in the Handles clause.
'The order is unimportant but ordering them you will Tab to them is not a bad idea.
Private Sub TextBoxes_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles Button1.PreviewKeyDown,
                                                                                             TextBox1.PreviewKeyDown,
                                                                                             TextBox4.PreviewKeyDown,
                                                                                             TextBox2.PreviewKeyDown,
                                                                                             TextBox5.PreviewKeyDown,
                                                                                             TextBox3.PreviewKeyDown,
                                                                                             TextBox6.PreviewKeyDown,
                                                                                             Button2.PreviewKeyDown
    If e.KeyCode = Keys.Tab Then
        'This array must contain all controls to behave as Tab stops in order and the first must be repeated at the end.
        Dim controls As Control() = {Button1, TextBox1, TextBox4, TextBox2, TextBox5, TextBox3, TextBox6, Button2, Button1}

        'Find the currently active control in the array.
        Dim currentControlIndex = Array.IndexOf(controls, ActiveControl)

        'Get the next control in the manual tab order.
        Dim nextControl = controls(currentControlIndex + 1)

        'Focus that next control.
        nextControl.Select()
    End If
End Sub

That code works for the following form, where TextBox1, TextBox2 and TextBox3 are in Panel1 and TextBox4, TextBox5 and TextBox6 are in Panel2:

Manual Tab Form

Comments