Tab page will not receive focus when using the tab button on the keyboard

271 views Asked by At

I have a tab control with two tab pages. I have set the tab index correctly on the tab control using visual studio tab order function. The tab stop property is set to true on the control. However, when I try to use the tab key on the keyboard, the second tab page will not gain focus. My question is how can I make the second tab page appear when you use the tab button on the keyboard?

1

There are 1 answers

0
大陸北方網友 On

Here is a workaround maybe you can refer to.

You can try to override ProcessCmdKey and call ActiveControl.TabIndex to check whether the current focus is on TabControl.

If the currently active control is TabControl, simulate "Key Right pressed" to switch TabPage.

// tabpage count
int count = 1;

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    bool baseResult = base.ProcessCmdKey(ref msg, keyData);

    if (keyData == Keys.Tab && ActiveControl.TabIndex == tabControl1.TabIndex)
    {
        if (count < tabControl1.TabPages.Count)
        {
            // simulate key right pressed
            SendKeys.Send("{RIGHT}");
            count++;
            return true;
        }
        else
        {
            // reset count and selected tabpage
            count = 1;
            tabControl1.SelectedIndex = 0;
        }
    }

    return baseResult;
}

Test result:

enter image description here