zoukankan      html  css  js  c++  java
  • WPF TextBox设置输入限制 用正则表达式方式

    只能输入数字的限制

    在XAML文件里

    <TextBox PreviewTextInput=
    "TextBox_PreviewTextInput"></TextBox>
    

    xaml.cs文件里

    private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        Regex re = new Regex("[^0-9.-]+");
    
        e.Handled = re.IsMatch(e.Text);
    }
    
    

    以上这种方式是以相反的方式来设置正则表达式,该正则表达式是不为数字的意思

    只能输入正整数和0

    xaml.cs文件里

    private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        Regex re = new Regex("^[1-9]+[0-9]*$");
    
        e.Handled = !re.IsMatch(e.Text);
    }
    

    以上这种方式是以正向的方式来设置正则表达式,但后面的match就应该取反

    不能输入汉字

    在xaml文件

    
     xmlns:input="clr-namespace:System.Windows.Input;assembly=PresentationCore"
    
    
    <TextBox input:InputMethod.IsInputMethodEnabled="False"></TextBox>
    

    这种方式限制输入法使用,就只能输入英文,数字,字符等,不能输入汉字

  • 相关阅读:
    Css的transform和transition
    移动端事件
    回流和重绘
    Swift更新至2.2版本 语言变化
    编程中遇到的 问题 总结
    NSNotificationCenter
    iOS中boolean、Boolean、BOOL、bool的区别
    推送的 代码实战写法
    MKNetworkKit的使用
    MKNetworkKit 的介绍
  • 原文地址:https://www.cnblogs.com/AlinaL/p/13710589.html
Copyright © 2011-2022 走看看