Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Forms, Nested TabControls with Groupbox tab order mystery (w/Illustrations)

I have a form with nested tab controls. We're trying to make the application keyboard-friendly, thus when you're accessing the inner tab control, we'd like Control-Tab to cycle through the inner tabs. For the most part this works. Starting here with the focus in TextBox A....

enter image description here

Control Tab will take us to the next tab as expected, with the focus in TextBox B:

enter image description here

Fantastic, just what we wanted. If "Gamma" and "Delta" are similar, I go right around back to Alpha again with Control-Tab.

However, we have a few tabs where all of the controls are inside of group boxes. This causes all kinds of problems. If I take the same example, but put the text box inside of a group box. Starting here with the focus in the TextBox A again:

enter image description here

Control-tab takes us to the next tab as we'd expect. (Curiously, the focus isn't anywhere visible...)

enter image description here

But then Control-tab again takes us to...

enter image description here

Speculation: This is because the tabbing from Alpha (with groupboxes) to Beta (with groupboxes) left Beta with no control with focus. The focus returned to the tab control itself (AlphaBetaGammaDelta) and Control-Tab was passed to the outer tab control (OneTwoThreeFour).

Now, I can fix this by catching the SelectedIndexChanged event for the inner tab control, and manually focusing the first focus-able control inside that tab each time it changes, but that seems so wrong and a maintenance headache if the controls get moved around.

    // Works, but I'm not crazy about it.
    private void tabControlABGD_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch(tabControlABGD.SelectedTab.Name)
        {
            case "tp_A":
                tb_a.Focus();
                break;
            case "tp_B":
                tb_b.Focus();
                break;
            case "tp_G":
                tb_g.Focus();
                break;
            case "tp_D":
                tb_d.Focus();
                break;
        }
    }

Messing with the "tab order" of the form -- no effect whatsoever.

What's the right way to fix this?

like image 292
Clinton Pierce Avatar asked Sep 23 '14 20:09

Clinton Pierce


1 Answers

Instead of a switch, try to simply call the SelectNextControl method:

void tabControlABGD_SelectedIndexChanged(object sender, EventArgs e) {
  this.SelectNextControl(tabControlABGD, true, true, true, true);
}
like image 108
LarsTech Avatar answered Oct 29 '22 06:10

LarsTech