在Winform(C#)中要实现限制Textbox只能输入数字,一般的做法就是在按键事件中处理,判断keychar的值。限制只能输入数字,小数点,Backspace,del这几个键。数字0~9所对应的keychar为48~57,小数点是46,Backspace是8。
拖一个Textbox到窗体上,添加OnKeyPress事件,在事件写判断的代码,只要判断不是这些键,设置e.Handled的值为true,就可以屏蔽输入。
1.判断是否为数字或Backspace,按下面这样写的话只能输入数字和Backspace,所以还得给代码添加些条件,还要能够输入小数点。
2.输入小数点。输入的小数要符合数字的格式,类似9.9.9这样的是不能够输入的。做法就是用float.TryParse来转换Textbox中之前和之后的值,然后比较两者的转换结果。
1.C#代码
在Winform
</>code
- private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
- {
- //数字0~9所对应的keychar为48~57,小数点是46,Backspace是8
- e.Handled = true;
- //输入0-9和Backspace del 有效
- if ((e.KeyChar >= 47 && e.KeyChar <= 58) || e.KeyChar == 8)
- {
- e.Handled = false;
- }
- if (e.KeyChar == 46) //小数点
- {
- if (textBox1.Text.Length <= 0)
- e.Handled = true; //小数点不能在第一位
- else
- {
- float f;
- if (float.TryParse(textBox1.Text + e.KeyChar.ToString(), out f))
- {
- e.Handled = false;
- }
- }
- }
- }
</>code
- 方法一:
- private void tBox_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar == 0x20) e.KeyChar = (char)0; //禁止空格键
- if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return; //处理负数
- if (e.KeyChar > 0x20)
- {
- try
- {
- double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
- }
- catch
- {
- e.KeyChar = (char)0; //处理非法字符
- }
- }
- }
- 方法二:
- private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
- {
- if(e.KeyChar!=8&&!Char.IsDigit(e.KeyChar))
- {
- e.Handled = true;
- }
- }
- 或者
- private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
- {
- if(e.KeyChar!='\b'&&!Char.IsDigit(e.KeyChar))
- {
- e.Handled = true;
- }
- }
- 方法三:
- private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
- {
- if(e.KeyChar!='\b')//这是允许输入退格键
- {
- if((e.KeyChar<'0')||(e.KeyChar>'9'))//这是允许输入0-9数字
- {
- e.Handled = true;
- }
- }
- }
- 方法四:
- private void textBox1_Validating(object sender, CancelEventArgs e)
- {
- const string pattern = @"^\d+\.?\d+{1}quot;;
- string content = ((TextBox)sender).Text;
- if (!(Regex.IsMatch(content, pattern)))
- {
- errorProvider1.SetError((Control)sender, "只能输入数字!");
- e.Cancel = true;
- }
- else
- errorProvider1.SetError((Control)sender, null);
- }
- 方法五:
- private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
- {
- if(e.KeyChar=='.' && this.textBox1.Text.IndexOf(".")!=-1)
- {
- e.Handled=true;
- }
- if(!((e.KeyChar>=48 && e.KeyChar<=57) || e.KeyChar=='.' || e.KeyChar==8))
- {
- e.Handled=true;
- }
- }
- 方法六:
- private void tbx_LsRegCapital_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))
- {
- e.Handled = true;//消除不合适字符
- }
- else if (Char.IsPunctuation(e.KeyChar))
- {
- if (e.KeyChar != '.' || this.textBox1.Text.Length == 0)//小数点
- {
- e.Handled = true;
- }
- if (textBox1.Text.LastIndexOf('.') != -1)
- {
- e.Handled = true;
- }
- }
- }
- 方法七:
- 利用ASCII码处理办法、
- {
- if ((e.KeyChar <= 48 || e.KeyChar >=57) && (e.KeyChar != 8) && (e.KeyChar != 46))
- e.Handled = true;
- ================48代表0,57代表9,8代表空格,46代表小数点
- }
C#中 textbox只允许整数输入
Key Press事件
</>code
- private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar >= 31 && (e.KeyChar < '0' || e.KeyChar > '9')) { e.Handled = true; }
- }
Textbox 内限定输入只有两位小数的数字
</>code
- private void 只允许输入两位小数数字(object sender, KeyPressEventArgs e)
- {
- //设置只允许输入 . - 0~9 否则无效
- if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != 45 && e.KeyChar != 46)
- e.Handled = true;
- //输入为负号时,只能输入一次且只能输入一次
- if (e.KeyChar == 45 && (((TextBox)sender).SelectionStart != 0 || ((TextBox)sender).Text.IndexOf("-") >= 0))
- e.Handled = true;
- //输入小数点时判断
- if (e.KeyChar == 46)
- {
- //负数判断
- if (((TextBox)sender).Text.IndexOf("-") >= 0)
- {
- //如果有-号 输入为小数点时,小数点只能在负号右边第二位以上才能输入.只能输入一次
- if (((TextBox)sender).SelectionStart <= 1 || ((TextBox)sender).Text.IndexOf(".") >= 0) e.Handled = true;
- }
- else
- {
- //第二位开始允许输入小数,且只能输入一次
- if (((TextBox)sender).SelectionStart <= 0 || ((TextBox)sender).Text.IndexOf(".") >= 0) e.Handled = true;
- }
- //限定小数点出现位置,防止出现三位以上小数的情况
- if (((TextBox)sender).SelectionStart < ((TextBox)sender).Text.Length - 2) e.Handled = true;
- }
- //限定只能输入两位小数
- if (e.KeyChar != '\b' && (((TextBox)sender).SelectionStart) > (((TextBox)sender).Text.LastIndexOf('.')) + 2 && ((TextBox)sender).Text.IndexOf(".") >= 0)
- e.Handled = true;
- //光标在小数点右侧时判断输入是否合规
- if (e.KeyChar != '\b' && ((TextBox)sender).SelectionStart >= (((TextBox)sender).Text.LastIndexOf('.')) && ((TextBox)sender).Text.IndexOf(".") >= 0)
- {
- //光标位于小数点右侧第一位时判断
- if ((((TextBox)sender).SelectionStart) == (((TextBox)sender).Text.LastIndexOf('.')) + 1)
- {
- if ((((TextBox)sender).Text.Length).ToString() == (((TextBox)sender).Text.IndexOf(".") + 3).ToString())
- e.Handled = true;
- }
- //光标位于小数点右侧第一位时判断
- if ((((TextBox)sender).SelectionStart) == (((TextBox)sender).Text.LastIndexOf('.')) + 2)
- { if ((((TextBox)sender).Text.Length - 3).ToString() == ((TextBox)sender).Text.IndexOf(".").ToString()) e.Handled = true;
- }
- }
- }
2.VB.net 代码
</>code
- Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
- e.Handled = True
- '输入0-9和回连键时有效
- If (e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = "" Then
- e.Handled = False
- End If
- '输入小数点情况
- If e.KeyChar = "." Then
- '小数点不能在第一位
- If TextBox1.Text.Length <= 0 Then
- e.Handled = True
- Else
- '小数点不在第一位
- Dim f As Double
- If Double.TryParse(TextBox1.Text + e.KeyChar, f) Then
- e.Handled = False
- End If
- End If
- End If
- End Sub
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛