8

I have a windows form application. On the form there are three groupboxs. Each groupbox contains some controls. Please see the image. form

There is a groupbox "flag" that contains a few checkboxs. "flag" is inside in "groupbox1". I used Tab key to go through each control but it doesn't work for checkboxs in "flag". I did set proper tabindex for each control.

It works for textboxs and buttons but checkboxs.

Why? Thanks for help.

EDIT

 // groupBox2
        // 
        this.groupBox2.Controls.Add(this.pictureBox10);
        this.groupBox2.Controls.Add(this.pictureBox9);
        this.groupBox2.Controls.Add(this.pictureBox8);
        this.groupBox2.Controls.Add(this.pictureBox7);
        this.groupBox2.Controls.Add(this.chkStoplight);
        this.groupBox2.Controls.Add(this.lblStoplight);
        this.groupBox2.Controls.Add(this.chkIsCount);
        this.groupBox2.Controls.Add(this.chkExceptionFlag);
        this.groupBox2.Controls.Add(this.chkIsActive);
        this.groupBox2.Controls.Add(this.lblIsActive);
        this.groupBox2.Controls.Add(this.lblExceptionFlag);
        this.groupBox3.Controls.Add(this.lblIsCount);
        this.groupBox2.Location = new System.Drawing.Point(16, 201);
        this.groupBox2.Name = "groupBox2";
        this.groupBox2.Size = new System.Drawing.Size(321, 70);
        this.groupBox2.TabIndex = 10;
        this.groupBox2.TabStop = true;
        this.groupBox2.Text = "Flags";

        // 
        // chkStoplight
        // 
        this.chkStoplight.AutoSize = true;
        this.chkStoplight.Location = new System.Drawing.Point(44, 25);
        this.chkStoplight.Name = "chkStoplight";
        this.chkStoplight.Size = new System.Drawing.Size(15, 14);
        this.chkStoplight.TabIndex = 0;
        this.chkStoplight.UseVisualStyleBackColor = true;

        In the property, I found TabStop is true for chkStoplight.

2 Answers2

14

For System.Windows.Forms.GroupBox:

You should make sure that your GroupBox flag has an appropriate TabIndex set.

From MSDN - How to: Set the Tab Order on Windows Forms:

Additionally, by default, a GroupBox control has its own TabIndex value, which is a whole number. A GroupBox control itself cannot have focus at run time. Thus, each control within a GroupBox has its own decimal TabIndex value, beginning with .0. Naturally, as the TabIndex of a GroupBox control is incremented, the controls within it will be incremented accordingly. If you changed a TabIndex value from 5 to 6, the TabIndex value of the first control in its group automatically changes to 6.0, and so on

Also, make sure the TabStop property of your GroupBox flag is not set to false. I believe false is the default.

For System.Windows.Controls GroupBox:

Make sure that the GroupBox.IsTabStop property is set. This also defaults to false.

Update: It appears that all of your controls are being added to groupBox3. You should make sure that each of them is being added only to its containing groupbox. For example, checkBox1, checkBox2, and checkBox3 should all be added to flag, which itself should be added to groupBox1. groupBox3 should only contain Back, Next, Finish, and Cancel.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Jon Senchyna
  • 7,867
  • 2
  • 26
  • 46
  • There is no TabStop property for GroupBox. –  Jun 15 '12 at 14:25
  • Check the link. It specifically goes to the MSDN articale for GroupBox.TabStop Property. In addition, I tested creating a new GroupBox and verified that a) the property exists and b) it defaults to false. – Jon Senchyna Jun 15 '12 at 14:26
  • Okay. Maybe checkbox is only for mouse rather than keystroke? –  Jun 15 '12 at 14:37
  • I updated my answer to include properties for both `System.Windows.Controls.GroupBox` and `System.Windows.Forms.GroupBox`. My original answer was based off of the assumption that you were using the Forms GroupBox, which might explain why I found there to be a TabStop property and you didn't. – Jon Senchyna Jun 15 '12 at 14:39
  • Your checkboxes should have `TabStop` (or `IsTabStop`) defaulted to true. I still think the issue is in your `flag` GroupBox not having its `TabStop` property appropriately set. – Jon Senchyna Jun 15 '12 at 14:40
  • I added it when I drag it from the toolbox. And it can be found in designer code as well. –  Jun 15 '12 at 15:03
  • I just saw that you updated your code example so that the checkboxes are in the correct groupbox. Were they originally in groupbox3? If so, correcting the code so that they are now in groupbox2 might fix things. – Jon Senchyna Jun 15 '12 at 15:04
  • Don't worry about groupbox name. Because there is a groupbox3 in the image. I changed it to avoid confusion. groupbox2 is for "flag". –  Jun 15 '12 at 15:06
2

I found that the only way to get the tab order in WinForms group boxes is by changing the order in which the controls are added to the group boxes in the generated InitializeControl method.

If you have multiple group boxes you will have to check the order in which the group boxes are added to their container and possibly change it.

I really dislike editing generated code but as far as I can see it is the only way to fix this.

Setting the TabStop property of the group box did not help at all.

Emond
  • 50,210
  • 11
  • 84
  • 115