加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 21:54:23 所属栏目:百科 来源:网络整理
导读:我有一个项目,我需要关注前一个字段,如果当前的一个是空的但用户继续删除.就像你在某处键入CD-Key一样.你有几个块,每个块有4-5个符号.例如,如果你删除第3个textBox,那么在第3个文本框变为emprty之后,你将被强制回到第二个textBox. if (textBox2.Text.Length
我有一个项目,我需要关注前一个字段,如果当前的一个是空的但用户继续删除.就像你在某处键入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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读