Results 1 to 1 of 1

Thread: [VB.NET] Use the Tab Key to Move Between TabPages in the TabControl

  1. #1

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    [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.
    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:
    1. ' Our very simple custom control.
    2. Public Class TabControlTabber
    3.     Inherits TabControl
    4.  
    5.     Protected Overrides Function IsInputKey( _
    6.         ByVal keyData As System.Windows.Forms.Keys) As Boolean
    7.  
    8.         If keyData = Keys.Tab Then
    9.             ' If this is the Tab key return True so
    10.             ' it can be handled as a regular input key.
    11.             Return True
    12.         End If
    13.  
    14.         Return MyBase.IsInputKey(keyData)
    15.     End Function
    16.  
    17. 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:
    1. Public Class Form1
    2.     ' In which direction should the TabPages be navigated.
    3.     Private forward As Boolean
    4.  
    5.     Private Sub TabControlTabber1_KeyDown(ByVal sender As System.Object, _
    6.         ByVal e As System.Windows.Forms.KeyEventArgs) _
    7.         Handles TabControlTabber1.KeyDown
    8.  
    9.         If e.KeyCode = Keys.Tab Then
    10.             ' Catch the Tab KeyDown.
    11.             With Me.TabControlTabber1
    12.  
    13.                 If .SelectedIndex = 0 Then
    14.                     ' We're at the first TabPage so move forward.
    15.                     forward = True
    16.                 ElseIf .SelectedIndex = .TabCount - 1 Then
    17.                     ' Now we're at the last TabPage. Turn around.
    18.                     forward = False
    19.                 End If
    20.  
    21.                 If forward Then
    22.                     ' If going forward then select the next TabPage.
    23.                     .SelectedIndex = .SelectedIndex + 1
    24.                 Else
    25.                     ' Moving backward. Select the previous TabPage.
    26.                     .SelectedIndex = .SelectedIndex - 1
    27.                 End If
    28.             End With
    29.         End If
    30.  
    31.     End Sub
    32.  
    33. 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:
    1. Private Sub TabControlTabber1_KeyDown(ByVal sender As System.Object, _
    2.     ByVal e As System.Windows.Forms.KeyEventArgs) _
    3.     Handles TabControlTabber1.KeyDown
    4.  
    5.     If e.KeyCode = Keys.Tab Then
    6.         ' Catch the Tab KeyDown.
    7.         With Me.TabControlTabber1
    8.  
    9.             If .SelectedIndex <> .TabCount - 1 Then
    10.                 ' We're not at the end yet.
    11.                 ' Keep moving through the TabPages.
    12.                 .SelectedIndex = .SelectedIndex + 1
    13.             Else
    14.                 ' We are at the last TabPage in this TabControl.
    15.                 ' Move to the next in the tab order.
    16.                 Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
    17.             End If
    18.         End With
    19.     End If
    20.  
    21. End Sub
    22.  
    23. Private Sub TabControlTabber1_Enter(ByVal sender As System.Object, _
    24.     ByVal e As System.EventArgs) Handles TabControlTabber1.Enter
    25.     ' Since we are back to the TabControl, let's
    26.     ' set the SelectedIndex to the first TabPage
    27.     ' so we can move through them again.
    28.     Me.TabControlTabber1.SelectedIndex = 0
    29. 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.
    Last edited by nmadd; Aug 3rd, 2007 at 07:53 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width