Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 1000
chama Avatar asked Dec 15 '10 17:12

chama


People also ask

How do I assign keyboard shortcuts?

To assign a keyboard shortcut do the following: Begin keyboard shortcuts with CTRL or a function key. Press the TAB key repeatedly until the cursor is in the Press new shortcut key box. Press the combination of keys that you want to assign.

How do I assign keyboard shortcuts in Visual Studio?

On the menu bar, choose Tools > Options. Expand Environment, and then choose Keyboard. Optional: Filter the list of commands by entering all or part of the name of the command, without spaces, in the Show commands containing box. In the list, choose the command to which you want to assign a keyboard shortcut.


1 Answers

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
    
like image 193
Cody Gray Avatar answered Oct 10 '22 00:10

Cody Gray