C# TabIndex 无法正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11052265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TabIndex does not work correctly
提问by
I have a windows form application. On the form there are three groupboxs.
Each groupbox contains some controls. Please see the image.

我有一个 Windows 窗体应用程序。在表单上有三个分组框。每个分组框包含一些控件。请看图片。

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.
有一个包含几个复选框的组框“标志”。“flag”在“groupbox1”里面。我使用 Tab 键浏览每个控件,但它不适用于“标志”中的复选框。我确实为每个控件设置了适当的 tabindex。
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.
采纳答案by Jon Senchyna
For System.Windows.Forms.GroupBox:
对于System.Windows.Forms.GroupBox:
You should make sure that your GroupBox flaghas an appropriate TabIndex set.
你应该确保你的 GroupBoxflag有一个合适的 TabIndex 集。
From MSDN - How to: Set the Tab Order on Windows Forms:
来自MSDN - 如何:在 Windows 窗体上设置 Tab 键顺序:
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
此外,默认情况下,GroupBox 控件具有自己的 TabIndex 值,该值是一个整数。GroupBox 控件本身在运行时不能具有焦点。因此,GroupBox 中的每个控件都有自己的十进制 TabIndex 值,从 .0 开始。自然地,随着 GroupBox 控件的 TabIndex 增加,其中的控件也会相应地增加。如果将 TabIndex 值从 5 更改为 6,则其组中第一个控件的 TabIndex 值会自动更改为 6.0,依此类推
Also, make sure the TabStopproperty of your GroupBox flagis not set to false. I believe false is the default.
此外,请确保GroupBox的TabStop属性flag未设置为 false。我相信 false 是默认值。
For System.Windows.Controls GroupBox:
对于System.Windows.Controls GroupBox:
Make sure that the GroupBox.IsTabStopproperty is set. This also defaults to false.
确保设置了GroupBox.IsTabStop属性。这也默认为 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 checkBox3should all be added to flag, which itself should be added to groupBox1. groupBox3should only contain Back, Next, Finish, and Cancel.
更新:您的所有控件似乎都被添加到groupBox3. 您应该确保它们中的每一个都仅被添加到其包含的 groupbox 中。例如,checkBox1、checkBox2、 和checkBox3都应该添加到 中flag,而 本身应该添加到 中groupBox1。 groupBox3应该只包含返回、下一步、完成和取消。
回答by Erno
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.
我发现在 WinForms 组框中获取 Tab 键顺序的唯一方法是在生成的 InitializeControl 方法中更改控件添加到组框中的顺序。
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.
设置组框的 TabStop 属性根本没有帮助。

