C#WPF焦点以前的textBox如果当前擦除

时间:2013-08-03 18:25:03

标签: c# wpf focus

我有一个项目需要关注前一个字段,如果当前字段为空但用户继续删除。就像你在某处键入CD-Key一样。你有几个块,每个块有4-5个符号。例如,如果你删除第3个textBox,那么在第3个文本框变为emprty之后,你将被强制回到第二个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) {...
}

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

private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
     if (e.Key == Key.Delete)
     {
          if (textBox2.Text.Length == 0)
          {
               Keyboard.Focus(textBox1);
          }
     }
}

3 个答案:

答案 0 :(得分:1)

这是任意数量的TextBox'es的通用源。

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;
}

使用PreviewKeyDown的第三个解决方案,仅支持Key.Delete:

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;
}

答案 1 :(得分:0)

最后。这个有效:

private void textBox2_KeyUp(object sender, KeyEventArgs e)
{
     if (e.Key == Key.Delete)
     {
          if (textBox2.Text.Length == 0)
          {
          Keyboard.Focus(textBox1);
          }
     }
}

答案 2 :(得分: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();
        }
    }                  
}