txtbox怎么能只能接受数字? 我建立一个textbox控件,准备用来接收用户输入的年份值,4位数字,该怎么做才能阻挡用户输入其他非数字字符.c#语言描述 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用验证控件或用JavaScript判断 //using System.Runtime.InteropServices; [DllImport("User32", CharSet=CharSet.Auto)] private static extern int SetWindowLong(IntPtr hWnd, int Index, int Value); [DllImport("user32", EntryPoint="GetWindowLong")] public static extern int GetWindowLongA(IntPtr hwnd, int nIndex); const int GWL_STYLE = -16; const int ES_NUMBER = 0x2000; private void Form1_Load(object sender, System.EventArgs e) { SetWindowLong(this.textBox1.Handle,GWL_STYLE, GetWindowLongA(this.textBox1.Handle,GWL_STYLE) | ES_NUMBER); } 用正则表达式限制只能输入数字:onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"四位的你自己改改就行了 string regex = @"^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))"; 这个是日期的正则你把月日去掉就可以了不知道你为什么不用下拉框这样省去好多麻烦 能不能用Keypress 事件来处理 在killfocus函数中:try{ textbox.Text转化为整型}catch{ MessageBox.Show("请输入数字"); textbox.Text = ""; textbox.focusd = true;}我怕麻烦,都是这么做的^_^,虽然不严谨,但还是很好用的 设置TextBox1.Numeric = true,即可限制只能输入数字。 using System;using System.Windows.Forms;using System.Runtime.InteropServices;class Test : Form{ const int GWL_STYLE = -16; const int ES_NUMBER = 0x2000; [DllImport("User32", CharSet=CharSet.Auto)] static extern int SetWindowLong(IntPtr hWnd, int Index, int Value); [DllImport("user32", EntryPoint="GetWindowLong")] static extern int GetWindowLongA(IntPtr hwnd, int Index); Test() { TextBox tbx = new TextBox(); tbx.Parent = this; tbx.MaxLength = 4; SetWindowLong(tbx.Handle, GWL_STYLE, GetWindowLongA(tbx.Handle,GWL_STYLE) | ES_NUMBER); } static void Main() { Application.Run(new Test()); }} 有个比较变态的方法,引用VB.NET 的dll。就可以用IsNumeric函数了 对不起,错了,设置TextBox1.Numeric = true只能用于移动控件。 没必要怎么麻烦吧!直接在keypress事件里添加个判断就行了! 你可以用VS自带的验证控件来进行验证或者,你就像楼上各位说的那样用DropDownList控件绑定年份,这样就不会输入其它的值了!就用验证控件吧。怪容易实现的。。 private void txtPrice_KeyPress(object sender,System.Windows.Forms.KeyPressEventArgs e){ if (char.IsNumber(e.KeyChar)==false) { if (e.KeyChar!=(char)46 && e.KeyChar!=(char)8) e.Handled =true; }}方法2 //处理消息 // protected override void WndProc(ref System.Windows.Forms.Message m) { int WM_CHAR = 0x0102; if(m.Msg == WM_CHAR) { if(((char)m.WParam >= '0') && ((char)m.WParam <= '9') || (int)m.WParam == (int)Keys.Back || (int)m.WParam == (int)Keys.Delete) { base.WndProc (ref m); } } else { base.WndProc (ref m); } } //文本默认为空 protected override void OnCreateControl() { base.OnCreateControl (); this.Clear(); } /// <summary> /// 判断字符串中含有指定的字符 /// </summary> /// <param name="text">指定的字符串</param> /// <param name="findChar">指定的字符</param> /// <returns></returns> public static bool ContainChar(string text, char findChar) { return text.IndexOf(findChar) > -1; } /// <summary> /// 判断当前输入的字符是否是合法数字字符 /// </summary> /// <param name="text">已经输入的数字内容</param> /// <param name="selectStart">光标在编辑控件中的起始位置</param> /// <param name="isSelectAll">判断当前编辑控件内容是否全选</param> /// <param name="e">按键时激活的事件类</param> public static void HandleNumKeyPress(string text, int selectStart, bool isSelectAll, KeyPressEventArgs e) { if (char.IsNumber(e.KeyChar)==false) { if ((!isSelectAll && (ContainChar(text, (char)46)) || e.KeyChar!=(char)46) && ((!isSelectAll && text != "" && selectStart != 0) || e.KeyChar!=(char)45) && e.KeyChar!=(char)8) { e.Handled =true; } } }然后在keypress事件中挂接PrjFundCommon.HandleNumKeyPress(((TextEdit)sender).Text,((TextEdit)sender).SelectionStart, ((TextEdit)sender).Text==((TextEdit)sender).SelectedText, e); 我这个是WEb页面上的textbox 客户端可以验证吗 使用JS比较好,直接客户端验证。function NumberOnly(){ /* Examples: <input type=text onkeydown=NumberOnly()> */ if (!((event.keyCode>=48 && event.keyCode<=57) || (event.keyCode>=96 && event.keyCode<=105) || event.keyCode==8 || event.keyCode==9 || event.keyCode==37 || event.keyCode==39 || event.keyCode==46)) event.returnValue=false if(event.shiftKey && (event.keyCode>=48 && event.keyCode<=57)) event.returnValue=false} 这个正则表达式匹配四位十进制数:"^\d{4}$"在C#里这样用:Regex r = new Regex(@"^\d{4}$");if (r.IsMatch(textBox1.Text)) ... 尝试读取或写入受保护的内存 关于数据绑定到Ilist<string>的问题 文本框默认值 那位大师进来帮忙看看难题 我想对 excle 进行操作......... 求助:请高手进来一下 在WinForm里的ListBox控件有没有Text和Value之分的呀? 结构体读写的问题? 那里买域名便宜一点,可以转域名 一个许多菜鸟关心的关于erp和crm编程设计的问题,请各位大虾赐教!!! PropertyGrid 颜色改变问题 请教C#安装文件制作 如何让TreeView的一个节点呈选择状态(获得焦点,而不是复选状态)
[DllImport("User32", CharSet=CharSet.Auto)] private
static extern int SetWindowLong(IntPtr hWnd, int Index, int Value);
[DllImport("user32", EntryPoint="GetWindowLong")] public
static extern int GetWindowLongA(IntPtr hwnd, int nIndex);
const int GWL_STYLE = -16;
const int ES_NUMBER = 0x2000; private void Form1_Load(object sender, System.EventArgs e)
{
SetWindowLong(this.textBox1.Handle,GWL_STYLE,
GetWindowLongA(this.textBox1.Handle,GWL_STYLE) | ES_NUMBER);
}
)|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[4
69])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\
s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([1
3579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((
0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((
0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9]
)|(2[0-8]))))))"; 这个是日期的正则
你把月日去掉就可以了不知道你为什么不用下拉框
这样省去好多麻烦
try
{
textbox.Text转化为整型
}
catch
{
MessageBox.Show("请输入数字");
textbox.Text = "";
textbox.focusd = true;
}我怕麻烦,都是这么做的^_^,虽然不严谨,但还是很好用的
using System.Windows.Forms;
using System.Runtime.InteropServices;class Test : Form
{
const int GWL_STYLE = -16;
const int ES_NUMBER = 0x2000; [DllImport("User32", CharSet=CharSet.Auto)]
static extern int SetWindowLong(IntPtr hWnd, int Index, int Value);
[DllImport("user32", EntryPoint="GetWindowLong")]
static extern int GetWindowLongA(IntPtr hwnd, int Index); Test()
{
TextBox tbx = new TextBox();
tbx.Parent = this;
tbx.MaxLength = 4;
SetWindowLong(tbx.Handle, GWL_STYLE, GetWindowLongA(tbx.Handle,GWL_STYLE) | ES_NUMBER);
} static void Main()
{
Application.Run(new Test());
}
}
直接在keypress事件里添加个判断就行了!
就用验证控件吧。怪容易实现的。。
{
if (char.IsNumber(e.KeyChar)==false)
{
if (e.KeyChar!=(char)46 && e.KeyChar!=(char)8)
e.Handled =true;
}
}
方法2
//处理消息
//
protected override void WndProc(ref System.Windows.Forms.Message m)
{
int WM_CHAR = 0x0102;
if(m.Msg == WM_CHAR)
{
if(((char)m.WParam >= '0') && ((char)m.WParam <= '9') ||
(int)m.WParam == (int)Keys.Back || (int)m.WParam == (int)Keys.Delete)
{
base.WndProc (ref m);
}
}
else
{
base.WndProc (ref m);
}
}
//文本默认为空
protected override void OnCreateControl()
{
base.OnCreateControl ();
this.Clear();
}
/// 判断字符串中含有指定的字符
/// </summary>
/// <param name="text">指定的字符串</param>
/// <param name="findChar">指定的字符</param>
/// <returns></returns>
public static bool ContainChar(string text, char findChar)
{
return text.IndexOf(findChar) > -1;
}
/// <summary>
/// 判断当前输入的字符是否是合法数字字符
/// </summary>
/// <param name="text">已经输入的数字内容</param>
/// <param name="selectStart">光标在编辑控件中的起始位置</param>
/// <param name="isSelectAll">判断当前编辑控件内容是否全选</param>
/// <param name="e">按键时激活的事件类</param>
public static void HandleNumKeyPress(string text, int selectStart, bool isSelectAll, KeyPressEventArgs e)
{
if (char.IsNumber(e.KeyChar)==false)
{
if ((!isSelectAll && (ContainChar(text, (char)46)) || e.KeyChar!=(char)46)
&& ((!isSelectAll && text != "" && selectStart != 0) || e.KeyChar!=(char)45)
&& e.KeyChar!=(char)8)
{
e.Handled =true;
}
}
}
然后在keypress事件中挂接PrjFundCommon.HandleNumKeyPress(((TextEdit)sender).Text,((TextEdit)sender).SelectionStart, ((TextEdit)sender).Text==((TextEdit)sender).SelectedText, e);
function NumberOnly()
{
/*
Examples:
<input type=text onkeydown=NumberOnly()>
*/
if (!((event.keyCode>=48 && event.keyCode<=57) || (event.keyCode>=96 && event.keyCode<=105) || event.keyCode==8 || event.keyCode==9 || event.keyCode==37 || event.keyCode==39 || event.keyCode==46))
event.returnValue=false
if(event.shiftKey && (event.keyCode>=48 && event.keyCode<=57)) event.returnValue=false
}
"^\d{4}$"
在C#里这样用:
Regex r = new Regex(@"^\d{4}$");
if (r.IsMatch(textBox1.Text))
...