How can I set up keyboard shortcuts for a Windows Forms TabControl?
Asked Answered
I

3

6

Is there an easy way to set a keyboard shortcut for a tab in a tab control in Visual Studio 2010? Is there some property I can set?

I looked online, but all the articles I saw were very confusing.

Immoral answered 15/12, 2010 at 17:33 Comment(3)
Windows Forms app? ASP.NET? WPF? Third party library of controls?Emf
Windows Form app using VB in the code behind.Immoral
Why isn't this closed yet? Do you need something else?Ancillary
P
6

Unfortunately, there isn't any such a property, but implementing this functionality doesn't have to be difficult, either. There are two ways worth considering, depending on the design of your application.

  1. If the form that hosts the TabControl already uses a menu system, it's almost trivial to set up. First, you need to add a menu command that switches to each TabPage in your TabControl. Then, you can simply add a keyboard shortcut to that menu item (which is a simple property of a MenuItem/ToolStripMenuItem), so that whenever that key is pressed, that menu command is executed, which switches to the appropriate TabPage.

  2. However, a menu system might not be appropriate for every form. If that's the case, you're going to have to do a bit more work. Basically, you need to set the KeyPreview property of the form that hosts your TabControl to True and detect the keyboard shortcuts you want to use to switch tabs.

    Setting a form's KeyPreview property allows that form to receive key events before those events are passed on to the control that has the focus. This is crucial for this method to work, because otherwise, your code in the form's KeyDown event handler will never detect the keystrokes that you want to trap. Only once the form has finished processing each keystroke will they be passed onto the control that would ordinarily receive them.

    So, once you've set this property, you need to add code to the handler for your form's KeyDown event that watches for whichever keyboard shortcuts you want to use, and then switches tabs accordingly if it detects that one of those keys is pressed. Otherwise, you don't have to do anything.

    For example, if you have three TabPages on your form, you might decide that F2 will switch to the first tab, F3 will switch to the second, and F4 will switch to the third (although, obviously, you could use whatever keys you wanted). You would then add the following code to your form's KeyDown event handler that detects those keys being depressed and acts accordingly:

     Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
           Case Keys.F2
              'Switch to the first tab page
              MyTabControl.SelectedIndex = 0
           Case Keys.F3
              'Switch to the second tab page
              MyTabControl.SelectedIndex = 1
           Case Keys.F4
              'Switch to the third tab page
              MyTabControl.SelectedIndex = 2
        End Select
     End Sub
    
Puncheon answered 18/12, 2010 at 14:42 Comment(0)
A
0

You can override a control's ProcessCmdKey() method to implement custom shortcut keystrokes. Big advantage of doing it this way is that the keystroke will work only when the tab control or one of the controls on the tab pages has the focus. Project + Add Class, paste the code shown below and compile. Drop the new control from the top of the toolbox onto your form.

Public Class MyTabControl
    Inherits TabControl

    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
        If keyData >= Keys.F1 And keyData <= Keys.F10 Then
            Me.SelectedIndex = keyData - Keys.F1
            Return True
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

End Class

If you want the function keys to work no matter what control on the form has the focus then you should override the ProcessCmdKey method of the form.

Ancillary answered 18/12, 2010 at 19:59 Comment(0)
C
-1

Winforms solution - handle the tab control's KeyDown event:

 Private Sub AppLaunchTabControl_KeyDown(sender As Object, e As KeyEventArgs) Handles AppLaunchTabControl.KeyDown

        If e.Modifiers = Keys.Alt Then

            If e.KeyCode = Keys.A Then
                AppLaunchTabControl.SelectTab("A TabPage")
                e.Handled = True
            ElseIf e.KeyCode = Keys.B Then
                AppLaunchTabControl.SelectTab("B TabPage")
                e.Handled = True
            ElseIf e.KeyCode = Keys.C Then
                AppLaunchTabControl.SelectTab("C TabPage")
                e.Handled = True
            End If

        End If
    End Sub

The only issue is underlining the hotkey letter in the tab page title by using ampersand: "&A TabPage" as mentioned in online references. It never displays as underlined for me, just displays the ampersand concatenated with the tab page name. Any further information on this particular issue would be welcome!

Circumcision answered 24/8, 2022 at 19:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.