8

Alt text

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 some way to do this?

I looked at ProcessDialogKey and IsInputKey, but they seem to work only with single keydata. Modifiers are not handled.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
tsps
  • 1,200
  • 3
  • 13
  • 25

3 Answers3

12

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);
  }
}
Jasper
  • 2,166
  • 4
  • 30
  • 50
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 4
    Note the code above doesn't disable all the tab control hotkeys: you can still use **CTRL+TAB+SHIFT** etc. Change the if statement to below: `if ( ke.Control && (ke.KeyCode == Keys.Tab || ke.KeyCode == Keys.Next || ke.KeyCode == Keys.Prior)) return;` – AZ. Jun 14 '12 at 17:04
  • That shortcut key tabs backwards. – Hans Passant Jun 14 '12 at 17:41
4

As written, Hans Passant's answer didn't work unless you were on the last page of the tab control. I want something the doesn't allow the shortcuts on any of the tabs. The code below also incorporates AZ's additional logic to handle Ctrl + PageUp and Ctrl + PageDown. Hopefully this version makes the overall logic a little more clear:

using System.Windows.Forms;

namespace MyNameSpace
{
    internal class NoTabTabControl : TabControl
    {
        /// <summary>
        /// Intercept any key combinations that would change the active tab.
        /// </summary>
        protected override void OnKeyDown(KeyEventArgs e)
        {
            bool changeTabKeyCombination =
                (e.Control
                    && (e.KeyCode == Keys.Tab
                        || e.KeyCode == Keys.Next
                        || e.KeyCode == Keys.Prior));

            if (!changeTabKeyCombination)
            {
                base.OnKeyDown(e);
            }
        }
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mark Meuer
  • 7,200
  • 6
  • 43
  • 64
0

Just change tabpageX.Enabled property to false in your code when required. Then using Ctrl + Tab will not be able to Select the tabpageX.

Ctrl + Tab at first look created a havoc on my application. I used this to save my assets.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vikas Kumar
  • 519
  • 5
  • 16