WPF文本框输入限制代码示例

        最近在写串口和CAN调试工具。为了使UI更友好,网上找了不少代码:)。要求CAN ID文本框内只能输入4个字符;CAN DATA文本框内只能输入八个字节的字符,每个字节字符之间用“-”分隔开。
        首先是在文本框的PreviewKeyUp事件处理器中屏蔽到空格,因为在PreviewTextInput事件处理器中用正则表达式也无法屏蔽掉空格。其中的小算法是同事搞定的 呵呵!很不错的小伙!上菜!

        public int step = 0;    
        private void AddCanData_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            Regex re = new Regex("[^0-9a-fA-F-]");
            e.Handled = re.IsMatch(e.Text);
            if (tb.Text.Length==0)
            {
                step = 0;
            }

            if (step==0)
            {
                if (e.Text.ToString() == "-")
                {
                    e.Handled = true;
                }
                else
                {
                    step = 1;
                    return ;
                }
            }

            if (step==1)
            {
                if (e.Text.ToString()=="-")
                {
                    step = 0;
                }
                else
                {
                    step = 2;
                }
                return;
            }

            if (step==2)
            {
                if (e.Text.ToString() == "-")
                {                                
                    step = 0;
                }
                else
                {
                    e.Handled = true;
                }
                return;
            }
        }
        private void AddCanID_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            bool yes;
            Regex re = new Regex("[^0-9a-fA-F]");
            yes = re.IsMatch(e.Text);
            if (yes==false)
            {
                if (tb.Text.Length>3)
                {
                    yes = true;
                }
            }
            e.Handled = yes;
        }
        private void AddCanID_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            var input = tb.Text.Trim();
            tb.Text = input;
        }
        private void AddCanData_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            var input = tb.Text.Trim();
            tb.Text = input;
            if (input.Count(c=>c=='-')>7)
            {
                tb.Text = input.Remove(input.Length-1, 1);
                btn_Add.Focus();
            }
        }

以上代码有BUG,更新如下。现在是真正的原创了 :)

        private void AddCanData_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            Regex re = new Regex("[^0-9a-fA-F-]");
            e.Handled = re.IsMatch(e.Text);

            MessageBox.Show($"tb.text:{tb.Text}" + $"  e.text:{e.Text}");

            if (tb.Text.Length==0)
            {
                if (e.Text=="-")
                {
                    e.Handled = true;
                }
            }
            else
            {
                string[] strArray = tb.Text.Split('-');
                string tbTextLastChar = tb.Text.Substring(tb.Text.Length - 1, 1);
                if (tbTextLastChar=="-")
                {
                    if (e.Text=="-")
                    {
                        e.Handled = true;
                    }
                }
                else
                {
                    if (strArray[strArray.Length - 1].Length >= 2)
                    {
                        if (e.Text != "-")
                        {
                            e.Handled = true;
                        }

                    if (strArray.Length==8)
                      {
                          if (e.Text=="-")
                          {
                              e.Handled = true;
                          }
                      }
                    }
                }
            }
        }
        private void AddCanID_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            bool yes;
            Regex re = new Regex("[^0-9a-fA-F]");
            yes = re.IsMatch(e.Text);
            if (yes==false)
            {
                if (tb.Text.Length>3)
                {
                    yes = true;
                }
            }
            e.Handled = yes;
        }
        private void AddCanID_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            var input = tb.Text.Trim();
            tb.Text = input;
        }
        private void AddCanData_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            var input = tb.Text.Trim();
            if (tb.Text.Contains(" "))
            {               
                tb.Text = input;
                btn_Add.Focus();
            }
        }

还是用空格隔开比较好,更符合习惯。

        private void CanData_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex("[^0-9a-fA-F]");
            e.Handled = re.IsMatch(e.Text);

            if (tb.Text.Length == 0)
            {
                if (e.Text == " ")
                {
                    e.Handled = true;
                }
            }
            else
            {
                string[] strArray = tb.Text.Split(' ');
                int count = 0;
                foreach (var item in strArray)
                {
                    if (item != "")
                    {
                        count++;
                    }
                }
                string tbTextLastChar = tb.Text.Substring(tb.Text.Length - 1, 1);

                if (count >7)
                {
                    if (tbTextLastChar != " ")
                    {
                        if (strArray[strArray.Length - 1].Length >= 2)
                        {
                            e.Handled = true;
                        }
                    }else
                    {
                        e.Handled = true;
                    }
                }
                else 
                {
                    if (tbTextLastChar == " ")
                    {
                        if (e.Text == " ")
                        {
                            e.Handled = true;
                        }
                    }
                    else
                    {
                        if (strArray[strArray.Length - 1].Length >= 2)
                        {
                            if (e.Text != " ")
                            {
                                e.Handled = true;
                            }
                        }
                    }
                }
            }
        }

//让 button 有点击过的感觉

                <Button Name="btTest" Height="24" Width="120" Click="Button_Click">
                    <TextBlock>
                        <TextBlock.Style>
                            <Style TargetType="TextBlock">
                                <Setter Property="Text" Value="测试!"/>
                                <Setter Property="FontWeight" Value="Heavy"/>
                                <Style.Triggers>
                                    <MultiTrigger>
                                        <MultiTrigger.Conditions>
                                            <Condition Property="IsEnabled" Value="false" />
                                            <Condition Property="IsMouseOver" Value="True" />
                                        </MultiTrigger.Conditions>
                                        <Setter Property="Foreground" Value="Red"/>
                                    </MultiTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                </Button>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值