Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In wpf, how can I get the next control in the tab order

I know how to set the focus to the next control in the tab order, but I don't actually want to change focus... I just want to get the next control in the tab order (perhaps get the previous, first and last ones too.) So... howyadodat?

M

like image 528
Mark A. Donohoe Avatar asked Apr 22 '11 14:04

Mark A. Donohoe


People also ask

How do you control tab order?

Change the tab order for controlsIn the Navigation Pane, right-click the form and then click Design View. On the Design tab, in the Tools group, click Tab Order. In the Tab Order dialog box, under Section, click the section you want to change.

How do I change the order of my TabIndex?

The only way to change this order is to reorder your HTML elements. tabindex itself can do 2 things: it sets the order of "focusable" elements. it makes element "focusable".

How do you arrange a tab sequence in Visual Basic?

Visual Studio provides a special tool, shown in Figure 2-9, that allows you to quickly set tab order. Just select View >■ Tab Order from the menu. You can then assign TabIndex values by clicking controls in the desired order. Label controls have a TabIndex setting even though they cannot receive focus.

What is tab order in accessibility?

The default keyboard navigation order must be logical and intuitive. The tab order should follow the visual flow of the page: left to right, top to bottom – header first, then main navigation, then page navigation (if present), and finally the footer.


1 Answers

PredictFocus(FocusNavigationDirection.Next) doesn't work as @Cameron said. My wrapper code based on @Randolpho 's post is now working well.

I tried several patterns and finally concluded I have to make sure container is actually one of the parents of e to avoid unexpected results.

/// <summary>
/// Get next tab order element.
/// </summary>
/// <param name="e">The element to get next tab order</param>
/// <param name="container">The container element owning 'e'. Make sure this is a container of 'e'.</param>
/// <param name="goDownOnly">True if search only itself and inside of 'container'; otherwise false.
/// If true and next tab order element is outside of 'container', result in null.</param>
/// <returns>Next tab order element or null if not found</returns>
public DependencyObject GetNextTab(DependencyObject e, DependencyObject container, bool goDownOnly)
{
    var navigation = typeof(FrameworkElement)
        .GetProperty("KeyboardNavigation", BindingFlags.NonPublic | BindingFlags.Static)
        .GetValue(null);

    var method = navigation
        .GetType()
        .GetMethod("GetNextTab", BindingFlags.NonPublic | BindingFlags.Instance);

    return method.Invoke(navigation, new object[] { e, container, goDownOnly }) as DependencyObject;
}

Ex.)

var nextElement = GetNextTab(textbox1, window, false);
like image 117
cactuaroid Avatar answered Nov 09 '22 04:11

cactuaroid