简体   繁体   中英

Jumping tab order using Control.Focus() when pressing Tab key (.net 3.5)

I have three text boxes:

<TextBox Name="textBox1" LostFocus="textBox1_LostFocus" />
<TextBox Name="textBox2" />
<TextBox Name="textBox3" />

With this event:

private void textBox1_LostFocus(object sender, RoutedEventArgs e)
{
    if (textBox1.Text.Equals("some value"))
        textBox3.Focus();
}

When I press TAB key with focus on textBox1, the focus goes to textBox2, independently of textBox3.Focus(). How could I really set focus on textBox3?

After some testing I found that you are currently catching the wrong event. Changing the first line of your XAML code into the following

<TextBox Name="textBox1" LostKeyboardFocus="textBox1_LostKeyboardFocus" />

and implementing the following method

private void textBox1_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) {

    if (textBox1.Text.Equals("some value")) {
        Keyboard.Focus(textBox3);
    }
}

the focus in the window is correctly set to the desired control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM