如何让TextBox只能输入数字、汉字、字母?

 

在编程的过程中我们可能经常会用到TextBox只接受数字输入(或者其他,比如:汉字,字母,等等),这个时候我们可能需要重新封装一个 TextBox(其他方法当然也可以),经常看到有人问这个问题,今天抽了一点时间将此封装做了一下,现在共享大家,希望能给大家带来一定的帮助,如有不 妥敬请斧正,另外:如需转载请注明出处: <script type="text/JavaScript"> alimama_pid="mm_10249644_1605763_5018464"; alimama_type="f"; alimama_sizecode ="tl_1x1_8"; alimama_fontsize=12; alimama_bordercolor="FFFFFF"; alimama_bgcolor="FFFFFF"; alimama_titlecolor="0000FF"; alimama_underline=0; alimama_height=22; alimama_width=0; </script> <script src="http://a.alimama.cn/inf.js" type="text/javascript"> </script>
只能输入数字(int型)的代码(直接复制就OK了)

  1. public class IntTextBox : System.Windows.Forms.TextBox
  2.     {
  3.         private int selectPos = 0;
  4.         public IntTextBox()
  5.             : base()
  6.         {
  7.             this.BackColor = Color.Beige;
  8.             this.TextChanged += new EventHandler(this.TextChage);
  9.             this.Leave += new EventHandler(this.FocusLeave);
  10.         }
  11.         //焦点发生改变时,此处你可以根据自己需要自己选择
  12.         public void FocusLeave(object sender, System.EventArgs e)
  13.         {
  14.             if (this.Text != "")
  15.             {
  16.                 this.Text = ToDBC(this.Text);
  17.                 if (!(new Regex(@"^-?/d+$")).IsMatch(this.Text))
  18.                 {
  19.                     this.BackColor = Color.OrangeRed;
  20.                     MessageBox.Show("输入内容不合法!""输入提示");
  21.                     this.Focus();
  22.                 }
  23.                 else
  24.                 {
  25.                     this.BackColor = Color.Beige;
  26.                 }
  27.             }
  28.         }

  29.         //内容发生改变时
  30.         public void TextChage(object sender, System.EventArgs e)
  31.         {
  32.             selectPos = this.SelectionStart;
  33.             if (this.Text != "")
  34.             {   
  35.                 //此处采用郑州表达式 <script type="text/JavaScript"> alimama_pid="mm_10249644_1605763_4930558"; alimama_type="f"; alimama_sizecode ="tl_1x5_8"; alimama_fontsize=12; alimama_bordercolor="FFFFFF"; alimama_bgcolor="FFFFFF"; alimama_titlecolor="0000FF"; alimama_underline=0; alimama_height=22; alimama_width=512; </script> <script src="http://a.alimama.cn/inf.js" type="text/javascript"> </script> name="alimamaf0.5063871357993794" id="alimamaf0.5063871357993794" border="0" marginwidth="0" marginheight="0" style="width: 512px; height: 22px;" src="http://z.alimama.com/alimama.php?i=mm_10249644_1605763_4930558&u=http%3A%2F%2Fblog.csdn.net%2Fgisfarmer%2Farchive%2F2009%2F01%2F07%2F3728214.aspx&w=512&h=22&re=1280x800&sz=tl_1x5_8&r=http%3A%2F%2Fblog.csdn.net%2Fgisfarmer%2Farchive%2F2009%2F01%2F08%2F3733153.aspx&cg=70468ddafbf023f71358745c6ff01e88&prq=91122852&cas=prq&cah=770&caw=1280&ccd=32&ctz=8&chl=2&cja=1&cpl=18&cmm=55&cf=10.0&dx=2&fu=-1&cbh=0&cbw=1390&iss=0&t=f&tc=0000FF&bgc=FFFFFF&bdc=FFFFFF&tlfs=12&tt=%E5%A6%82%E4%BD%95%E8%AE%A9TextBox%E5%8F%AA%E8%83%BD%E8%BE%93%E5%85%A5%E6%95%B0%E5%AD%97%E3%80%81%E6%B1%89%E5%AD%97%E3%80%81%E5%AD%97%E6%AF%8D%EF%BC%9F%20-%20%E5%8D%8A%E6%94%AF%E7%83%9F%E9%98%BF%E6%9D%B0%20-%20CSDNBlog&sx=545&sy=992&ac=7042&pf=1" scrolling="no" frameborder="0">
  36.                 if (!(new Regex(@"^-?/d+$")).IsMatch(this.Text))
  37.                 {
  38.                     this.BackColor = Color.OrangeRed;
  39.                     this.SelectionStart = selectPos;
  40.                 }
  41.                 else
  42.                 {
  43.                     this.BackColor = Color.Beige;
  44.                     this.SelectionStart = selectPos;
  45.                 }
  46.             }
  47.             else
  48.             {
  49.                 this.BackColor = Color.Beige;
  50.             }
  51.         }

  52.         //全角转换成半角,此功能也可以选择
  53.         public string ToDBC(string input)
  54.         {
  55.             char[] c = input.ToCharArray();
  56.             for (int i = 0; i < c.Length; i++)
  57.             {
  58.                 if (c[i] == 12288)
  59.                 {
  60.                     c[i] = (char)32;
  61.                     continue;
  62.                 }
  63.                 if (c[i] > 65280 && c[i] < 65375)
  64.                     c[i] = (char)(c[i] - 65248);
  65.             }
  66.             return new string(c);
  67.         }
  68.     }

下面代码是只能输入浮点数(至于其他的比如汉字、字母等等原理一样) <script type="text/JavaScript"> alimama_pid="mm_10249644_1605763_4930558"; alimama_type="f"; alimama_sizecode ="tl_1x5_8"; alimama_fontsize=12; alimama_bordercolor="FFFFFF"; alimama_bgcolor="FFFFFF"; alimama_titlecolor="0000FF"; alimama_underline=0; alimama_height=22; alimama_width=512; </script> <script src="http://a.alimama.cn/inf.js" type="text/javascript"> </script>

  1.     public class FloatTextBox : System.Windows.Forms.TextBox
  2.     {
  3.         private int selectPos = 0;
  4.         public FloatTextBox()
  5.             : base()
  6.         {
  7.             this.BackColor = Color.Beige;
  8.             this.TextChanged += new EventHandler(this.TextChage);
  9.             this.Leave += new EventHandler(this.FocusLeave);
  10.         }
  11.         //焦点发生改变时
  12.         public void FocusLeave(object sender, System.EventArgs e)
  13.         {
  14.             if (this.Text != "")
  15.             {
  16.                 this.Text = ToDBC(this.Text);
  17.                 if (!(new Regex(@"^/d+(/./d+)?$")).IsMatch(this.Text))
  18.                 {
  19.                     this.BackColor = Color.OrangeRed;
  20.                     MessageBox.Show("输入内容不合法!""输入提示");
  21.                     this.Focus();
  22.                 }
  23.                 else
  24.                 {
  25.                     this.BackColor = Color.Beige;
  26.                 }
  27.             }
  28.         }

  29.         //内容发生改变时
  30.         public void TextChage(object sender, System.EventArgs e)
  31.         {
  32.             selectPos = this.SelectionStart;
  33.             if (this.Text != "")
  34.             {
  35.                 if (!(new Regex(@"^/d+(/./d+)?$")).IsMatch(this.Text))
  36.                 {
  37.                     this.BackColor = Color.OrangeRed;
  38.                     this.SelectionStart = selectPos;
  39.                 }
  40.                 else
  41.                 {
  42.                     this.BackColor = Color.Beige;
  43.                     this.SelectionStart = selectPos;
  44.                 }
  45.             }
  46.             else
  47.             {
  48.                 this.BackColor = Color.Beige;
  49.             }
  50.         }

  51.         //全角转换成半角
  52.         public string ToDBC(string input)
  53.         {
  54.             char[] c = input.ToCharArray();
  55.             for (int i = 0; i < c.Length; i++)
  56.             {
  57.                 if (c[i] == 12288)
  58.                 {
  59.                     c[i] = (char)32;
  60.                     continue;
  61.                 }
  62.                 if (c[i] > 65280 && c[i] < 65375)
  63.                     c[i] = (char)(c[i] - 65248);
  64.             }
  65.             return new string(c);
  66.         }
  67.     }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值