BLOG.CSHARPHELPER.COM: Let the user select a TabControl tab by pressing accelerator keys such as Alt-B in C#
Let the user select a TabControl tab by pressing accelerator keys such as Alt-B in C#
A TabControl's tabs cannot display accelerator keys so the user cannot press Alt-X to open the X tab. In fact, the tabs cannot even display underlined characters. This example shows how to provide these features.
First make an owner-drawn TabControl that draws the tabs with underlined accelerator keys. (Note that this example always displays the accelerator keys underlined. Some operating systems only underline these keys after the user presses the Alt key.) For more information on owner-drawn TabControls, see Make a TabControl use owner-drawn tabs and let the user close tabs at run time in C#.
Next set the form's KeyPreview property to True. In the form's KeyDown event handler, look for the TabControl's accelerators and select the appropriate tabs. This example looks for Alt-B, Alt_L, and Alt-D.
When it finds an accelerator, the program gives the TabControl focus, selects the appropriate tab page, and suppresses the key press so the form doesn't beep.
Comments