微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有一个项目,我需要关注前一个字段,如果当前的一个是空的但用户继续删除.就像你在某处键入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);
          }
     }
}

解决方法

这是任意数量的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;
}

使用仅支持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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐