开发者

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

开发者 https://www.devze.com 2023-03-03 19:11 出处:网络
I have three text boxes: <TextBox Name=\"textBox1\" LostFocus=\"textBox1_LostFocus\" /> <TextBox 开发者_如何学GoName=\"textBox2\" />

I have three text boxes:

<TextBox Name="textBox1" LostFocus="textBox1_LostFocus" />
<TextBox 开发者_如何学GoName="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.

0

精彩评论

暂无评论...
验证码 换一张
取 消