1
votes

I have a tabcontrol with a series of tabs that contain textboxes and other input controls. If I click on the tab headers I can navigate through the tabs using the left and right arrow keys, but if I'm currently in a textbox or other control then I can't, assuming this is as the textbox takes all of the key events. I have tried attaching event handlers to keydown and previewkeydown but they don't get fired.

Is there a way to get the key events through the tabcontrol even when a child control has focus?

1
Hold down the CTRL key and press the left or right arrow key to switch tabs. Standard OS behavior, all tab controls in any program behave that way, don't change it.Hans Passant
CTRL + left/right didn't do anything but CTRL + TAB works! Thanks.mbdavis

1 Answers

0
votes

You can use the TabControl's KeyDown event to listen for arrow keys.

But this will likely conflict with other controls: e.g. Moving the cursor left/right in a TextBox.

If all the controls are read-only, then this won't be much of an issue. But if people are allowed to alter the data, then intercepting the arrow keys will interfere with standard navigation.

    private void tabControl1_KeyDown(object sender, KeyEventArgs e) {
        if (e.KeyCode == Keys.Left) {
            if (tabControl1.SelectedIndex > 0) {
                tabControl1.SelectedIndex--;
            }
        };
        if (e.KeyCode == Keys.Right) {
            if (tabControl1.SelectedIndex < tabControl1.TabCount - 1) {
                tabControl1.SelectedIndex++;
            }
        };
    }

Note: Having done a bit more testing, it works fine with TabSheets containing TextBoxes, but doesn't work if RadioButtons have focus in a TabSheet.