C#winform的textbox中怎么验证输入是数字 在C# winform中的textbox中怎么验证输入的是不是数字,或者就在输入的不是数字是提示并且阻止用户输入 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果能用asp.net中的验证控件就好了 在KEYDOWN事件里,使用正则表达式验证下,若不是数字,则不要它。 var id=document.getElementById("控件ID");if(id!(^[-9]+$)){alert("必须是数字");}你试下if(id!(^[-9]+$))的括号加不加双引我忘了加的话是这样if(id!("^[-9]+$")) var id=document.getElementById("控件ID").value; if(id!(^[-9]+$)) { alert("必须是数字"); } 你试下 if(id!(^[-9]+$))的括号加不加双引我忘了 加的话是这样if(id!("^[-9]+$"))少加了 value private string pattern = @"^[0-9]*$";private string param1 = null; private void textBoxParam1_TextChanged(object sender, EventArgs e) { Match m = Regex.Match(this.textBoxParam1.Text, pattern); // 匹配正则表达式 if (!m.Success) // 输入的不是数字 { this.textBoxParam1.Text = param1; // textBox内容不变 // 将光标定位到文本框的最后 this.textBoxParam1.SelectionStart = this.textBoxParam1.Text.Length; } else // 输入的是数字 { param1 = this.textBoxParam1.Text; // 将现在textBox的值保存下来 } } 那是用 javascript的如果单是 asp.net 的页面类里写是这样的string txtname=TextBox1.Text;if (txtname!= ("^[-9]+$")){ Response.Write("<script>alert('必须是数字');</script>"); return;}我试过没问题 最简单 无耻 小程序 直接int a =0;try{ a=int.parse(this.textBox1.text);}catch(...){ 输出:不是数值} 倒 人家要的是 在C# winform中 加分吧 /// <summary> /// 判断输入的字符串只包含数字,可以匹配整数和浮点数 /// </summary> public static bool IsNumber(string input) { string pattern = "^-?\\d+$|^(-?\\d+)(\\.\\d+)?$"; Regex regex = new Regex(pattern); return regex.IsMatch(input); }这是个方法 1、用try...catch是大忌2、小数怎么办? 说了 简单 无耻的了嘛 还找毛病,不给面子。小数,你自己提前不知道 是不是 整数?小数?那float 直接用 numericUpDown 控件。用什么text ,真菜 呵呵,恭喜你了, 正好我现在在做一个医院的体检系统,其中有不少地方要限制是输入数字的,下面是我自己创建的一个类。你只需要在 需要限制的Textbox控件的 TextChanged 事件中引用 该类,将变量更改为当前 控件名 就可以了。限制的想法是:当文本框内的内容发生改变时,如果按键所对应的结果非数字时,则不显示出来。反之则显示。 /// <summary> /// ----------------------限制textbox内只能输入数字----------------- /// </summary> /// <param name="e">需要限制的控件名称</param> public void TextBox_OnlyNumber(System.Windows.Forms.TextBox e) { string s = ""; for(int i=0; i<e.Text.Length; i++) if(Char.IsDigit(e.Text[i]) == true) s = s + e.Text[i].ToString(); e.Text = s; e.SelectionStart = e.Text.Length; } 对了,如果你不是要重复使用,而只是用于某一个地方的时候,你只需要把 { ……}里面的内容复制到需要 限制 输入 的 Textbox 控件的TextChanged事件中,将参数e 更改成 当前控件名称就OK 了。 private void textBox4_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if ((e.KeyChar< 48||e.KeyChar>57)&&(e.KeyChar!=8)) { e.Handled = true; base.OnKeyPress(e); MessageBox.Show("输入月份必须数字!","提示"); } } 我不知道如果 一个商用软件中,如果用户按错了一个 键,你还每次都给他提示出来,那么我想请问一下,这样的软件 方便了用户吗??为何不直接将这个非数字字符直接剔除掉呢???而省略掉那些没有丝毫实际意义的 MessageBox.Show 呢???!!! private string pattern = @"^[0-9]*$";private string param1 = null; private void textBoxParam1_TextChanged(object sender, EventArgs e) { Match m = Regex.Match(this.textBoxParam1.Text, pattern); // 匹配正则表达式 if (!m.Success) // 输入的不是数字 { this.textBoxParam1.Text = param1; // textBox内容不变 // 将光标定位到文本框的最后 this.textBoxParam1.SelectionStart = this.textBoxParam1.Text.Length; } else // 输入的是数字 { param1 = this.textBoxParam1.Text; // 将现在textBox的值保存下来 } } if(!isnumber(textbox1.text)){ messagebox.show("请输入数字!");} using System.ComponentModel;using System.Windows.Forms;namespace TestTextBox{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void textBox1_Validating(object sender, CancelEventArgs e) { this.errorProvider1.Clear(); try { double.Parse(this.textBox1.Text); } catch { this.errorProvider1.SetError(this.textBox1, "请输入一个数字"); e.Cancel = true; } } }} 哈哈,楼主看不明白的....人家回答的不是数字是写不进去的.不想提示直接把人家里面的MessageBox 去掉不就好了? try { Decimal amt = Decimal.Parse(textBoxAMT.Text);//此处可以任意数值类型 } catch (FormatException ex) { String test = ex.ToString(); MessageBox.Show("请输入有效数字", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } 一伙人看了题目主乱答。23和28楼是正解,楼主的意思应该是输入的字符不是数字就不让输入,当然也就不让显示。所以应该在KEYPRESS事件中处理掉。弹消息框太霸道了一些,还是用errorProvider比较好 TRY CATCH的性能是个大问题,能提前预料的,就不要CATCH 应该在KEYPRESS事件中处理掉,同感 多个DevGrid导出到Excel中 江湖救急~资料笔数大於3000笔以上效能低落 image控件里的tooltip属性什么意思 如何接收键盘上的方向键输入 C# 编辑保存csv文件? datagrid 中数据邦定不了 请问属性的本质是什么?是否有函数调用的开销? 请问高手,如何设置SOCKET的超时时间,和如何捕获SOCKET的超时异常!!!!!!!!!!!!!!!! 如何能得到两个DateTime类型数据的相差天数? 使用Attributes设置TextBox1的Left,怎么不见位置变化? 关于固定时间开始多线程问题 用TTS实现发声,出现COM组件错误。
if(id!(^[-9]+$))
{
alert("必须是数字");
}你试下
if(id!(^[-9]+$))的括号加不加双引我忘了
加的话是这样if(id!("^[-9]+$"))
if(id!(^[-9]+$))
{
alert("必须是数字");
} 你试下
if(id!(^[-9]+$))的括号加不加双引我忘了
加的话是这样if(id!("^[-9]+$"))少加了 value
private string param1 = null;
private void textBoxParam1_TextChanged(object sender, EventArgs e)
{
Match m = Regex.Match(this.textBoxParam1.Text, pattern); // 匹配正则表达式 if (!m.Success) // 输入的不是数字
{
this.textBoxParam1.Text = param1; // textBox内容不变 // 将光标定位到文本框的最后
this.textBoxParam1.SelectionStart = this.textBoxParam1.Text.Length;
}
else // 输入的是数字
{
param1 = this.textBoxParam1.Text; // 将现在textBox的值保存下来
}
}
如果单是 asp.net 的页面类里写是这样的
string txtname=TextBox1.Text;if (txtname!= ("^[-9]+$"))
{
Response.Write("<script>alert('必须是数字');</script>");
return;
}我试过没问题
int a =0;
try
{
a=int.parse(this.textBox1.text);
}
catch(...)
{
输出:不是数值
}
/// 判断输入的字符串只包含数字,可以匹配整数和浮点数
/// </summary>
public static bool IsNumber(string input)
{
string pattern = "^-?\\d+$|^(-?\\d+)(\\.\\d+)?$";
Regex regex = new Regex(pattern);
return regex.IsMatch(input);
}
这是个方法
1、用try...catch是大忌
2、小数怎么办?
你只需要在 需要限制的Textbox控件的 TextChanged 事件中引用 该类,将变量更改为当前 控件名 就可以了。限制的想法是:当文本框内的内容发生改变时,如果按键所对应的结果非数字时,则不显示出来。反之则显示。
/// <summary>
/// ----------------------限制textbox内只能输入数字-----------------
/// </summary>
/// <param name="e">需要限制的控件名称</param>
public void TextBox_OnlyNumber(System.Windows.Forms.TextBox e)
{
string s = "";
for(int i=0; i<e.Text.Length; i++)
if(Char.IsDigit(e.Text[i]) == true)
s = s + e.Text[i].ToString();
e.Text = s;
e.SelectionStart = e.Text.Length;
}
……
}里面的内容复制到需要 限制 输入 的 Textbox 控件的TextChanged事件中,
将参数e 更改成 当前控件名称就OK 了。
{
if ((e.KeyChar< 48||e.KeyChar>57)&&(e.KeyChar!=8))
{
e.Handled = true;
base.OnKeyPress(e);
MessageBox.Show("输入月份必须数字!","提示");
}
}
我不知道如果 一个商用软件中,如果用户按错了一个 键,你还每次都给他提示出来,那么我想请问一下,这样的软件 方便了用户吗??为何不直接将这个非数字字符直接剔除掉呢???而省略掉那些没有丝毫实际意义的 MessageBox.Show 呢???!!!
private string param1 = null;
private void textBoxParam1_TextChanged(object sender, EventArgs e)
{
Match m = Regex.Match(this.textBoxParam1.Text, pattern); // 匹配正则表达式 if (!m.Success) // 输入的不是数字
{
this.textBoxParam1.Text = param1; // textBox内容不变 // 将光标定位到文本框的最后
this.textBoxParam1.SelectionStart = this.textBoxParam1.Text.Length;
}
else // 输入的是数字
{
param1 = this.textBoxParam1.Text; // 将现在textBox的值保存下来
}
}
{
messagebox.show("请输入数字!");
}
using System.ComponentModel;
using System.Windows.Forms;namespace TestTextBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void textBox1_Validating(object sender, CancelEventArgs e)
{
this.errorProvider1.Clear();
try
{
double.Parse(this.textBox1.Text);
}
catch
{
this.errorProvider1.SetError(this.textBox1, "请输入一个数字");
e.Cancel = true;
}
}
}
}
哈哈,楼主看不明白的....
人家回答的不是数字是写不进去的.不想提示直接把人家里面的MessageBox 去掉不就好了?
{ Decimal amt = Decimal.Parse(textBoxAMT.Text);//此处可以任意数值类型
}
catch (FormatException ex)
{
String test = ex.ToString(); MessageBox.Show("请输入有效数字", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
应该在KEYPRESS事件中处理掉,同感