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
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.
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".
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With