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.
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.
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.
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
.
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
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.
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!
© 2022 - 2024 — McMap. All rights reserved.