4

我有一个项目,如果当前字段为空但用户继续删除,我需要关注上一个字段。就像您在某处键入 CD-Key 一样。您有几个块,每个块有 4-5 个符号。例如,如果您擦除第 3 个文本框,您将在第 3 个文本框变空后立即被迫返回到第二个文本框。

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) {...
}

请帮帮我。UPD。我尝试了另一种解决方案,但它不起作用:

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'es列表的初始化:

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'es。要解决此问题,请使用 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 的第三个解决方案:

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:

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

最后。这个有效:

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 回答
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 回答