限制textbox只能输入数字?
来源:互联网 发布:note station windows 编辑:程序博客网 时间:2024/05/16 08:30
如何限制textbox只能输入数字?
如何限制textbox只能输入数字?
解决方案 »
0 0
- 限制TextBox只能输入数字
- 限制textbox只能输入数字?
- 限制TextBox中只能输入数字
- C#Winform限制Textbox只能输入数字
- TextBox限制只能输入字母或数字
- textBox只能输入数字
- TextBox只能输入数字
- TextBox只能输入数字
- textbox 只能输入数字
- C# TextBox限制只能输入数字(不包括点号)
- C# 限制TextBox控件中只能输入数字
- WPF C# VS2013 TextBox控件 限制只能输入数字
- 限制只能输入数字
- 让textbox只能输入数字
- 只能输入数字的TextBox
- c# Textbox 只能输入数字
- textbox只能输入数字和.
- C#textbox只能输入数字
- Arrays_Strings 判断字符串中的字符是否唯一@CareerCup
- 斐波那契(Fibonacci)数列
- linux shell实现随机数多种方法(date,random,uuid)
- 数据库索引应用(ms-sql)
- 物联网中的RPL路由协议(一)----概述
- 限制textbox只能输入数字?
- Latex处理jpg图片(转)
- 框架Struts的action中编写的基于session的购物车逻辑代码
- SQL 左外连接,右外连接,全连接,内连接
- 提高数据库处理查询速度
- 关于@property 声明变量与自己声明变量问题续
- hibernate关联查询
- Android自定义属性时TypedArray的使用方法
- platform模型驱动中断和内存资源使用步骤
<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中写onkeypress="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