This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Base Classes - Control - Tab Order

The twenty-eighth part of the Windows Presentation Foundation Fundamentals tutorial begins an examination of the Control class. This is a base class for all controls that include a replaceable template. The first article considers the tab order.

Changing the Tab Order

The tab order is controlled by two things. The most important is the TabIndex property of each control; a property provided by the Control class. By default, every control has a TabIndex of the largest possible 32-bit integer. Lowering the value moves a control closer to the start of the tab order. Only when two controls have the same tab index does the logical tree determine which is visited first.

To demonstrate, let's adjust the tab indexes of the text boxes so that they are selected first when tabbing. Change the XAML of the labels and text boxes as follows:

<Label>First Name</Label>
<TextBox Grid.Column="1" Height="22" TabIndex="0"/>

<Label Grid.Row="1">Initials</Label>
<TextBox Name="Test" Grid.Column="1" Grid.Row="1" Height="22" TabIndex="1"/>

<Label Grid.Row="2">Last Name</Label>
<TextBox Grid.Column="1" Grid.Row="2" Height="22" TabIndex="2"/>

Now when you run the program and tab between controls you should see a more logical tab order. The text boxes are selected first and the button is the last control to receive the focus.

Removing Controls from the Tab Order

In a small number of situations you might want to prevent a control from being focussed using the tab key. You should consider this very carefully though, as some users find it difficult to use other input devices and you should not limit their use of your software unnecessarily. However, if you do wish to remove a control from the tab order you can do so by setting its IsTabStop property to false.

For example, to prevent tabbing to the button you can replace its XAML with the following:

<Button DockPanel.Dock="Bottom"

Run the program for a final time and try tabbing through the controls. You should see that pressing tab whilst the Last Name text box is focussed moves the cursor into the First Name text box. The button is skipped.

26 September 2013