zoukankan      html  css  js  c++  java
  • TextBox限制输入字母、数字、退格键

    公共方法如下:

          /// <summary>
            /// 正则表达式验证只能输入数字或字母
            /// </summary>
            /// <param name="pendingString"></param>
            /// <returns></returns>
            public bool IsNaturalOrNumber(string str)
            {
                System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[A-Za-z0-9-]+$");
                return reg1.IsMatch(str);
            }
    
            /// <summary>
            /// 正则表达式验证只能输入数字
            /// </summary>
            /// <param name="pendingString"></param>
            /// <returns></returns>
            public bool IsNumber(string str)
            {
                System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[0-9-]+$");
                return reg1.IsMatch(str);
            }
    
            /// <summary>
            /// 正则表达式验证只能输入字母
            /// </summary>
            /// <param name="pendingString"></param>
            /// <returns></returns>
            public bool IsNatural(string str)
            {
                System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[A-Za-z-]+$");
                return reg1.IsMatch(str);
            }
    

    应用如下:

         private void textBox1_TextChanged(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty((sender as TextBox).Text)) return;
                if (!IsNumber((sender as TextBox).Text))
                {
                    this.textBox1.Text = this.textBox1.Text.Substring(0, this.textBox1.Text.Length - 1);
                    this.textBox1.SelectionStart = this.textBox1.Text.Length;
                    return;
                }
            }
    

      

  • 相关阅读:
    修改python默认版本
    Ansible基础
    day21
    paramiko上传文件到Linux
    参考书籍
    C++解析三
    块设备
    assert用法
    块设备驱动2
    块设备驱动1
  • 原文地址:https://www.cnblogs.com/YYkun/p/9181503.html
Copyright © 2011-2022 走看看