1.正则表达式做验证: Regex reg = new Regex(@"^[1-9]\d*$"); ///不匹配 if (!reg.IsMatch(this.Txt_Int.Text, 0))
2.在textBox中写οnkeypress="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());
}
}
效果相同
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也不能粘贴其它
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
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator" ControlToValidate="TextBox1" ValidationExpression='"^[0-9]*$"'></asp:RegularExpressionValidator>
这样....
{
if (!char.IsNumber(e.KeyChar) && ((e.KeyChar != '.') && (e.KeyChar != '\b')))
{
e.Handled = true;
}
}
1 建立微软的验证控件,在控件中填入上面的正则表达式2 或者写到前台的js脚本中,调用自己写的javascript函数来进行验证
2.在textBox中写οnkeypress="return event.keyCode>=48&&event.keyCode<=57||event.keyCode==46"
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 下通用,还带提示.
改了下更简单:)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());
}
}
效果相同
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 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也不能粘贴其它
只能输入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]*$”
'*************************************
'禁止输入非数字
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