4

現在のフィールドが空の場合に前のフィールドにフォーカスする必要があるプロジェクトがありますが、ユーザーは削除を続けます。どこかに 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);
          }
     }
}
4

3 に答える 3

1

これは、任意の量の TextBox の一般的な解決策です。

TextBox のリストの初期化:

private readonly List<TextBox> _textBoxes;

public MainWindow()
{
    InitializeComponent();

    _textBoxes = new List<TextBox> { _textBox1, _textBox2, _textBox3 };
}

KeyUp イベントのあるバージョン:

private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Tab)
        return;

    var current = (TextBox)sender;
    if (current.Text.Any())
        return;

    var index = _textBoxes.IndexOf(current);
    if (index == 0)
        return;

    var previous = _textBoxes[index - 1];
    previous.Focus();
    previous.CaretIndex = previous.Text.Length;
}

上記のバージョンでは、プレス アンド ホールドのシナリオで TextBox をジャンプすることが禁止されています。これを回避するには、TextChanged イベントを使用します。

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    var current = (TextBox)sender;
    if (current.Text.Any())
        return;

    var index = _textBoxes.IndexOf(current);
    if (index == 0)
        return;

    var previous = _textBoxes[index - 1];
    previous.Focus();
    previous.CaretIndex = previous.Text.Length;
}

Key.Delete のみをサポートする PreviewKeyDown を使用した 3 番目のソリューション:

private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key != Key.Delete)
        return;

    var current = (TextBox)sender;
    if (current.Text.Length != 0)
        return;

    var index = _textBoxes.IndexOf(current);
    if (index == 0)
        return;

    var previous = _textBoxes[index - 1];
    previous.Focus();
    previous.CaretIndex = 0;
}

Key.Delete と Key.Back の両方をサポートする PreviewKeyDown を使用した 4 番目のソリューション:

private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key != Key.Delete && e.Key != Key.Back)
        return;

    var current = (TextBox)sender;
    if (current.Text.Length != 0)
        return;

    var index = _textBoxes.IndexOf(current);
    if (index == 0)
        return;

    var previous = _textBoxes[index - 1];
    previous.Focus();

    if (e.Key == Key.Delete)
        previous.CaretIndex = 0;
}
于 2013-08-03T20:00:43.967 に答える
0

これはテストされていませんが、動作するはずです。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }        

    private void textBox2_KeyDown(object sender, KeyEventArgs e)
    {
        if (textBox2.Text == "")
        {
            textBox1.Focus();
        }
    }

    private void textBox3_KeyDown(object sender, KeyEventArgs e)
    {
        if (textBox3.Text == "")
        {
            textBox2.Focus();
        }
    }                  
}
于 2013-08-03T19:40:53.217 に答える
0

ついに。これは機能します:

private void textBox2_KeyUp(object sender, KeyEventArgs e)
{
     if (e.Key == Key.Delete)
     {
          if (textBox2.Text.Length == 0)
          {
          Keyboard.Focus(textBox1);
          }
     }
}
于 2013-08-03T19:26:39.483 に答える