現在のフィールドが空の場合に前のフィールドにフォーカスする必要があるプロジェクトがありますが、ユーザーは削除を続けます。どこかに CD-Key を入力したときのように。それぞれ 4 ~ 5 個のシンボルを持つブロックがいくつかあります。たとえば、3 番目の textBox を消去すると、3 番目の textBox が空になった直後に 2 番目の textBox に強制的に戻されます。
if (textBox2.Text.Length == 0)
{
Keyboard.Focus(textBox1);
}
このコードは正常に動作しますが、別の onfocus イベントがあることを考慮すると、フォーカスを取得するとすぐに textBox2 が空になり、フォーカスの上のコードが textBox1 に強制的に戻されます。だからループしている。
うまくいけば、削除ボタンを押すのをキャッチする必要がありますよね? しかし、ここに私の問題があります。このコードの挿入方法がわかりません
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
if (textBox2.Text.Length == 0)
{
Keyboard.Focus(textBox1);
}
}
}
この関数内:
private void textBox2_TextChanged(object sender, TextChangedEventArgs e)
{
if (textBox2.Text.Length == 2)
{
Keyboard.Focus(textBox3);
}
// HERE I NEED SOMETHING LIKE ELSE IF (e.Key == Key.Delete) {...
}
お願い助けて。アップデート。もう1つの解決策を試しましたが、うまくいきません:
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
if (textBox2.Text.Length == 0)
{
Keyboard.Focus(textBox1);
}
}
}