如何:在 Windows Forms 上設定定位順序How to: Set the tab order on Windows Forms

定位順序是使用者按下 Tab 鍵將焦點從一個控制項移至另一個控制項的順序。The tab order is the order in which a user moves focus from one control to another by pressing the Tab key. 每個表單都有自己的定位順序。Each form has its own tab order. 根據預設,定位順序與您建立控制項的順序相同。By default, the tab order is the same as the order in which you created the controls. 定位字元順序編號的開頭為零。Tab-order numbering begins with zero.

設定控制項的定位順序To set the tab order of a control

  1. 在 Visual Studio 的 [ View ] 功能表上,選取 [定位順序]In Visual Studio, on the View menu, select Tab Order.

    這會啟動表單上的索引標籤順序選取模式。This activates the tab-order selection mode on the form. 數位(代表 TabIndex 屬性)會出現在每個控制項的左上角。A number (representing the TabIndex property) appears in the upper-left corner of each control.

  2. 依序按一下控制項,以建立您想要的定位順序。Click the controls sequentially to establish the tab order you want.

    注意

    控制項在定位順序內的位置可以設定為任何大於或等於0的值。A control's place within the tab order can be set to any value greater than or equal to 0. 發生重複時,會評估這兩個控制項的迭置順序,並將頂端的控制項設為優先。When duplicates occur, the z-order of the two controls is evaluated and the control on top is tabbed to first. ([迭置順序] 是表單上控制項的視覺分層,沿著表單的 Z 軸 [深度]。(The z-order is the visual layering of controls on a form along the form's z-axis [depth]. 迭置順序會決定哪些控制項位於其他控制項前面)。如需有關迭置順序的詳細資訊,請參閱Windows Forms 上的分層物件The z-order determines which controls are in front of other controls.) For more information on z-order, see Layering Objects on Windows Forms.

  3. 當您完成時,請再次選取 [ View ] 功能表上的 [定位順序] ,離開定位順序模式。When you have finished, select Tab Order on the View menu again to leave tab order mode.

    注意

    無法取得焦點的控制項,以及停用和隱藏的控制項都沒有 TabIndex 屬性,而且不會包含在定位順序中。Controls that cannot get the focus, as well as disabled and invisible controls, do not have a TabIndex property and are not included in the tab order. 當使用者按下 Tab 鍵時,會略過這些控制項。As a user presses the Tab key, these controls are skipped.

或者,您也可以使用 TabIndex 屬性,在屬性視窗中設定定位順序。Alternatively, tab order can be set in the Properties window using the TabIndex property. 控制項的 [TabIndex] 屬性會決定它在定位順序中的位置。The TabIndex property of a control determines where it is positioned in the tab order. 根據預設,第一個繪製的控制項的 TabIndex 值為0,第二個控制項的 TabIndex 為1,依此類推。By default, the first control drawn has a TabIndex value of 0, the second has a TabIndex of 1, and so on.

此外,根據預設,GroupBox 控制項有自己的 TabIndex 值,也就是整數。Additionally, by default, a GroupBox control has its own TabIndex value, which is a whole number. GroupBox 控制項本身在執行時間不能有焦點。A GroupBox control itself cannot have focus at run time. 因此,GroupBox 中的每個控制項都有自己的十進位 TabIndex 值,從. 0 開始。Thus, each control within a GroupBox has its own decimal TabIndex value, beginning with .0. 自然地,隨著 GroupBox 控制項的 TabIndex 遞增,其內的控制項也會隨之增加。Naturally, as the TabIndex of a GroupBox control is incremented, the controls within it will be incremented accordingly. 如果您將 TabIndex 值從5變更為6,則其群組中第一個控制項的 TabIndex 值會自動變更為6.0,依此類推。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.

最後,您可以在定位順序中略過許多表單上的任何控制項。Finally, any control of the many on your form can be skipped in the tab order. 通常,在執行時間連續按 Tab 鍵,會選取定位順序中的每個控制項。Usually, pressing Tab successively at run time selects each control in the tab order. 藉由關閉 [TabStop] 屬性,您就可以在表單的定位順序中傳遞控制項。By turning off the TabStop property, you can make a control be passed over in the tab order of the form.

若要從定位順序中移除控制項To remove a control from the tab order

在 [屬性] 視窗中,將控制項的 [TabStop] 屬性設定為 [ false ]。Set the control's TabStop property to false in the Properties window.

如果控制項的 TabStop 屬性已設定為,false 仍會維持其在定位順序中的位置,即使在使用 Tab 鍵迴圈流覽控制項時略過控制項也一樣。A control whose TabStop property has been set to false still maintains its position in the tab order, even though the control is skipped when you cycle through the controls with the Tab key.

注意

選項按鈕群組在執行時間會有一個定位停駐點。A radio button group has a single tab stop at run time. 選取的按鈕(也就是其 Checked 屬性設定為 [true] 的按鈕)會自動將其 TabStop 屬性設定為 [true],而另一個按鈕的 [TabStop] 屬性會設定為 [false]。The selected button (that is, the button with its Checked property set to true) has its TabStop property automatically set to true, while the other buttons have their TabStop property set to false. 如需將 RadioButton 控制項分組的詳細資訊,請參閱將Windows Forms 選項按鈕控制項分組以做為集合For more information about grouping RadioButton controls, see Grouping Windows Forms RadioButton Controls to Function as a Set.

另請參閱See also