在编程的过程中我们可能经常会用到TextBox只接受数字输入(或者其他,比如:汉字,字母,等等),这个时候我们可能需要重新封装一个TextBox(其他方法当然也可以),经常看到有人问这个问题,今天抽了一点时间将此封装做了一下,现在共享大家,希望能给大家带来一定的帮助,如有不妥敬请斧正,另外:如需转载请注明出处: 半支烟阿杰。http://blog.youkuaiyun.com/gisfarmer/ 谢谢。
只能输入数字(int型)的代码(直接复制就OK了)
- public class IntTextBox : System.Windows.Forms.TextBox
- {
- private int selectPos = 0;
- public IntTextBox()
- : base()
- {
- this.BackColor = Color.Beige;
- this.TextChanged += new EventHandler(this.TextChage);
- this.Leave += new EventHandler(this.FocusLeave);
- }
- //焦点发生改变时,此处你可以根据自己需要自己选择
- public void FocusLeave(object sender, System.EventArgs e)
- {
- if (this.Text != "")
- {
- this.Text = ToDBC(this.Text);
- if (!(new Regex(@"^-?/d+$")).IsMatch(this.Text))
- {
- this.BackColor = Color.OrangeRed;
- MessageBox.Show("输入内容不合法!", "输入提示");
- this.Focus();
- }
- &