Ctrl + PageUp/PageDown and Ctrl + Tab are default shortcuts for the TabControl. They help in moving between adjacent tabs. I would like Ctrl + PageX behaviour to work only for the Outer Tabs (tab1, tab2) and Ctrl + Tab behaviour for the Inner Tabs (tab3, tab4) when my focus is in the control (textbox here). For this I need to disable the default behaviour. Is there someway to do this? I looked at ProcessDialogKey and IsInputKey but they seem to work only with single keydata. Modifiers are not handled.
+3
A:
TabControl has unusual keyboard shortcut processing, they are reflected to the OnKeyDown() method. This was done to avoid it disturbing the keyboard handling for the controls on a tab page. You'll have to override the method. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.
using System;
using System.Windows.Forms;
class MyTabControl : TabControl {
protected override void OnKeyDown(KeyEventArgs e) {
if (e.KeyData == (Keys.Tab | Keys.Control) ||
e.KeyData == (Keys.PageDown | Keys.Control)) {
// Don't allow tabbing beyond last page
if (this.SelectedIndex == this.TabCount - 1) return;
}
base.OnKeyDown(e);
}
}
Hans Passant
2009-12-23 13:48:19
Thanks very much. That works perfectly.
tsps
2009-12-25 06:56:46