TextBox
来源:互联网 发布:unity3d室内灯光开关 编辑:程序博客网 时间:2024/05/22 13:59
只能输入数字(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();
- }
- else
- {
- this.BackColor = Color.Beige;
- }
- }
- }
- //内容发生改变时
- public void TextChage(object sender, System.EventArgs e)
- {
- selectPos = this.SelectionStart;
- if (this.Text != "")
- {
- //此处采用郑州表达式
- if (!(new Regex(@"^-?/d+$")).IsMatch(this.Text))
- {
- this.BackColor = Color.OrangeRed;
- this.SelectionStart = selectPos;
- }
- else
- {
- this.BackColor = Color.Beige;
- this.SelectionStart = selectPos;
- }
- }
- else
- {
- this.BackColor = Color.Beige;
- }
- }
- //全角转换成半角,此功能也可以选择
- public string ToDBC(string input)
- {
- char[] c = input.ToCharArray();
- for (int i = 0; i < c.Length; i++)
- {
- if (c[i] == 12288)
- {
- c[i] = (char)32;
- continue;
- }
- if (c[i] > 65280 && c[i] < 65375)
- c[i] = (char)(c[i] - 65248);
- }
- return new string(c);
- }
- }
下面代码是只能输入浮点数(至于其他的比如汉字、字母等等原理一样)
- public class FloatTextBox : System.Windows.Forms.TextBox
- {
- private int selectPos = 0;
- public FloatTextBox()
- : 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+(/./d+)?$")).IsMatch(this.Text))
- {
- this.BackColor = Color.OrangeRed;
- MessageBox.Show("输入内容不合法!", "输入提示");
- this.Focus();
- }
- else
- {
- this.BackColor = Color.Beige;
- }
- }
- }
- //内容发生改变时
- public void TextChage(object sender, System.EventArgs e)
- {
- selectPos = this.SelectionStart;
- if (this.Text != "")
- {
- if (!(new Regex(@"^/d+(/./d+)?$")).IsMatch(this.Text))
- {
- this.BackColor = Color.OrangeRed;
- this.SelectionStart = selectPos;
- }
- else
- {
- this.BackColor = Color.Beige;
- this.SelectionStart = selectPos;
- }
- }
- else
- {
- this.BackColor = Color.Beige;
- }
- }
- //全角转换成半角
- public string ToDBC(string input)
- {
- char[] c = input.ToCharArray();
- for (int i = 0; i < c.Length; i++)
- {
- if (c[i] == 12288)
- {
- c[i] = (char)32;
- continue;
- }
- if (c[i] > 65280 && c[i] < 65375)
- c[i] = (char)(c[i] - 65248);
- }
- return new string(c);
- }
- }
- TextBox
- textbox
- textbox
- TextBox
- TextBox
- textbox
- TextBox
- textBox multiline
- stretch textbox
- 遍历TextBox
- TextBox专题
- 关于textbox
- TextBOx开发
- TextBox设置
- TextBox组件
- textbox样式
- textBox控件
- WaterMark TextBox
- OSGI 之 Declarative Service (1)
- WinDbg关联dump文件
- C++内存对齐机制
- 解决ADB server didn't ACK * failed to start daemon *
- 基于Jetty的内嵌式服务器
- TextBox
- QT中文支持(1)
- CSP的类型
- MIT科技评论10大新兴技术
- WizFi210 快速启动指导(版本1.0)(二)
- (转)HBase技术介绍
- C语言文件操作函数open、write用法速记
- removeView(view)不能移除控件问题
- java三个开源框架简介