[VB.NET] Use the Tab Key to Move Between TabPages in the TabControl
You have a Form that consists of a TabControl. You'd like to navigate between its TabPages with the keyboard. You know that you can do this with the arrow keys, but you really, really, really love the Tab key and you want to use that instead. :bigyello:
So, how can you catch the special Tab key and, in this instance, make it cycle through your TabPages in your TabControl?
The IsInputKey Function
We'll need to override this function in our very simple custom TabControl. We need to tell our control to treat the Tab key as just a regular old input key so we can do what we like when it is pressed. This is the key. This only takes a few lines of code.
VB.NET Code:
' Our very simple custom control.
Public Class TabControlTabber
Inherits TabControl
Protected Overrides Function IsInputKey( _
ByVal keyData As System.Windows.Forms.Keys) As Boolean
If keyData = Keys.Tab Then
' If this is the Tab key return True so
' it can be handled as a regular input key.
Return True
End If
Return MyBase.IsInputKey(keyData)
End Function
End Class
Tabbing Through the TabPages
Now we can handle the KeyDown event of the Tab key and do what we like. Here we will just cycle through the TabPages of our custom TabControl. The only thing that we really need to consider in this case is if we are going forward or backward through the TabPages.
VB.NET Code:
Public Class Form1
' In which direction should the TabPages be navigated.
Private forward As Boolean
Private Sub TabControlTabber1_KeyDown(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles TabControlTabber1.KeyDown
If e.KeyCode = Keys.Tab Then
' Catch the Tab KeyDown.
With Me.TabControlTabber1
If .SelectedIndex = 0 Then
' We're at the first TabPage so move forward.
forward = True
ElseIf .SelectedIndex = .TabCount - 1 Then
' Now we're at the last TabPage. Turn around.
forward = False
End If
If forward Then
' If going forward then select the next TabPage.
.SelectedIndex = .SelectedIndex + 1
Else
' Moving backward. Select the previous TabPage.
.SelectedIndex = .SelectedIndex - 1
End If
End With
End If
End Sub
End Class
Tab Between TabPages Then to the Next Control
What if we have multiple controls on our form? It doesn't make much sense to cycle through all of the TabPages and ignore all of the other controls on the form. We just need to change the code a little so that when we reach the last TabPage we move to the next control in the tab order instead of back through the pages. We'll reset the selected TabPage to the first one when we get back to the TabControl as well.
VB.NET Code:
Private Sub TabControlTabber1_KeyDown(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles TabControlTabber1.KeyDown
If e.KeyCode = Keys.Tab Then
' Catch the Tab KeyDown.
With Me.TabControlTabber1
If .SelectedIndex <> .TabCount - 1 Then
' We're not at the end yet.
' Keep moving through the TabPages.
.SelectedIndex = .SelectedIndex + 1
Else
' We are at the last TabPage in this TabControl.
' Move to the next in the tab order.
Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
End If
End With
End If
End Sub
Private Sub TabControlTabber1_Enter(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles TabControlTabber1.Enter
' Since we are back to the TabControl, let's
' set the SelectedIndex to the first TabPage
' so we can move through them again.
Me.TabControlTabber1.SelectedIndex = 0
End Sub
That's it.
A lot of work to just use the Tab key to move through your TabPages? Probably. However, you can of course use this with whatever you like. Perhaps it will give you some ideas for other controls.
Is this how the Tab key was meant to be used? Probably not. However, I wanted it to work this way for one instance so I tried it.
Good luck.