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>
    

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

  • 相关阅读:
    电脑连接树莓派Pi Zero W
    HTTP 302报文
    解决跨域访问
    转chromeUI4
    转chromeUI3
    转chromeUI2
    转chromeUI
    OPM中细节设置
    CMFCButton导致PropertySheet窗口关闭
    [转]objectarx 加载菜单-ObjectARX中右键(快捷)菜单的实现方法
  • 原文地址:https://www.cnblogs.com/AlinaL/p/13710589.html
Copyright © 2011-2022 走看看