限制textbox只能输入数字?
2014-02-27 10:51
204 查看
如何限制textbox只能输入数字?
如何限制textbox只能输入数字?
解决方案 »
捕获键盘事件,判断键盘上按下的是什么键,如果不是数字键就returnhttp://topic.csdn.net/u/20090420/14/50ee45c5-95d6-40e9-bd4b-71cbd80f6b53.html
参考C# WinForm开发系列 - TextBox
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator" ControlToValidate="TextBox1" ValidationExpression='"^[0-9]*$"'></asp:RegularExpressionValidator>
这样....
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsNumber(e.KeyChar) && ((e.KeyChar != '.') && (e.KeyChar != '\b')))
{
e.Handled = true;
}
}
写好 验证数字的正则表达式然后
1 建立微软的验证控件,在控件中填入上面的正则表达式2 或者写到前台的js脚本中,调用自己写的javascript函数来进行验证
1.正则表达式做验证: Regex reg = new Regex(@"^[1-9]\d*$"); ///不匹配 if (!reg.IsMatch(this.Txt_Int.Text, 0))
2.在textBox中写onkeypress="return event.keyCode>=48&&event.keyCode<=57||event.keyCode==46"
使用验证控件或检查键盘输入的ASCLL码
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Runtime.InteropServices;
class NumberTextBox:TextBox
{
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd,int nIndex, int dwNewLong);
[DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hWnd,int nIndex);
const int GWL_STYLE = (-16);
const int ES_NUMBER = 0x2000;
protected override void OnCreateControl ()
{
base.OnCreateControl ();
//将ES_NUMBER添加到窗体样式
int style= GetWindowLong (Handle,GWL_STYLE);
SetWindowLong (Handle,GWL_STYLE,style|ES_NUMBER);
}
}
class test:Form
{
public test()
{
Text = "Number Only TextBox";
NumberTextBox nt = new NumberTextBox();
Controls.Add (nt);
}
static void Main ()
{
Application.EnableVisualStyles();
Application.Run (new test());
}
}本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ChrisAK/archive/2008/12/27/3623467.aspx
windows 下通用,还带提示.
上面那个用到了API复杂了点.
改了下更简单:)using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Runtime.InteropServices;
class NumberTextBox:TextBox
{
const int ES_NUMBER = 0x2000;
protected override CreateParams CreateParams
{
get{
CreateParams ret = base.CreateParams;
ret.Style |= ES_NUMBER;
return ret;
}
}
}
class test:Form
{
public test()
{
Text = "Number Only TextBox";
NumberTextBox nt = new NumberTextBox();
Controls.Add (nt);
}
static void Main ()
{
Application.EnableVisualStyles();
Application.Run (new test());
}
}
效果相同
在TextBox的改变事件里写入正则校验或者ASCII比对都可以....
//判断字符是否为数字
public static bool StrIsInt(string Str)
{
try
{
Int32.Parse(this.textbox1.Text);
return true;
}
catch
{
this.label1.Text="请输入数字";
return false;
}
} 回答者: zengwei61 - 经理 四级 2008-10-13 08:47
Option Strict Off
Option Explicit On
Friend Class Form1
Inherits System.Windows.Forms.Form
Private Sub Text1_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles Text1.KeyPress
Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> 8 Then
KeyAscii = 0
End If
eventArgs.KeyChar = Chr(KeyAscii)
If KeyAscii = 0 Then
eventArgs.Handled = True
End If
End Sub
End Class
这个可行,已测试,CTRL+C也不能粘贴其它
只能输入数字:“^[0-9]*$”
只能输入n位的数字:“^d{n}$”
只能输入至少n位数字:“^d{n,}$”
只能输入m-n位的数字:“^d{m,n}$”
只能输入零和非零开头的数字:“^(0|[1-9][0-9]*)$”
只能输入有两位小数的正实数:“^[0-9]+(.[0-9]{2})?$”
只能输入有1-3位小数的正实数:“^[0-9]+(.[0-9]{1,3})?$”
只能输入非零的正整数:“^+?[1-9][0-9]*$”
只能输入非零的负整数:“^-[1-9][0-9]*$”
Private Sub txtUserID_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtUserID.KeyPress
'*************************************
'禁止输入非数字
Dim Str As String = txtUserID.Text
If e.KeyChar = Chr(13) Then
e.Handled = True
ElseIf (e.KeyChar < "0" Or e.KeyChar > "9") And e.KeyChar <> Chr(8) Then
e.Handled = True
End If
End Sub
Private Sub txtUserID_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtUserID.TextChanged
'************************************
'输入数字最大值不超过255
Dim Temp As String
If Val(txtUserID.Text) > 255 Then
txtUserID.Text = ""
Else
Temp = txtUserID.Text
End If
End Sub
相关文章推荐
- WPF C# VS2013 TextBox控件 限制只能输入数字
- C# TextBox限制只能输入数字(不包括点号)
- C# textbox中输入时加限制条件 // C#Winform下限制TextBox只能输入数字 // 才疏学浅(TextBox 小数点不能在首位+只能输入数字)
- TextBox限制只能输入字母或数字
- Win Form中限制TextBox只能输入数字
- C#验证:限制TextBox只能输入数字并控制输入数字的长度(数字)
- C#Winform限制Textbox只能输入数字
- C#Winform限制Textbox只能输入数字
- asp.net中前台限制TextBox文本框只能输入数字和小数(或数字和字母)
- asp.net 限制textbox只能输入数字,并且防止启用输入法来输入字母或汉字
- C#Winform下用正则表达式限制TextBox只能输入数字
- c#中设置像数量,价格,金额等的textbox的限制条件,用户只能输入数字或小数
- c#中设置像数量,价格,金额等的textbox的限制条件,用户只能输入数字或小数(小数及负号只能输一次)
- 关于正则表达式的使用一例。在Textbox 对话框内限制只能输入数字,如果输入出错,则清空内容。
- 限制TextBox只能输入数字
- C#&nbsp;限制TextBox控件中只能输入数字
- WinForm 限制TextBox只能输入数字和小数点
- WINFORM限制textbox只能输入数字[转]
- C#限制TextBox只能输入数字
- C#如何让TEXTBOX只能输入英文状态下的数字并且限制位数