急求!!关于TextBox的text属性输入的限制? 如何限制用户在TextBox控件中只能输入:0.1到0.9 这9个小数中的一个。即输入的数的值是小于1的 先谢了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 try{ decimal a=convert.ToDecimal(TextBox1.Text); if(a<0.1 || a>0.9) return;}catch{ return;} 在textBox的textChanged事件中限制输入字符为0-9 和小数点:private textBox_textChanged(object sender,System.EventArgs e){ for(int i=0;i<textBox1.text.length;i++) { if(textBox1.text[i]!=".") { if(!Char.IsDigit(textBox1.text[i])) { MessageBox.Show("不能输入数字和小数点以外的任何字符","提示"); textBox1.text=""; return; } } }}在textBox的validating事件中检测输入值的大小:private void textBox_Validating(object sender, System.ComponentModel.CancelEventArgs e){ if(textBox.text!="") { double inputValue=Convert.toDouble(textBox.text); if(inputValue>0.9 || inputValue<0.1) { MessageBox.Show("输入数字应在0.1到0.9之间,请重新输入!","提示"); textBox.SelectAll(); return; } }}为了使当输入值不符范围时,仍能正常关闭窗体,请将窗体的causeValidation属性设置为false 你可以在TextBox的Leave事件中进行判断,判断当前输入是否合法,如果不合法,返回焦点进行修改 liudng(大东) 的方法我试过,消息异常回卡一下,你们的也是吗? Regex reg = new Regex("0\\.[1-9]$");if( !reg.IsMatch(this.TextBox1.Text)) this.TextBox1.Text = string.Empty; private void textBox1_TextChanged_1(object sender, System.EventArgs e) {// using System.Text.RegularExpressions; string pat = string.Empty; Regex rg = null; Match mh = null; if(this.textBox1.Text.Length == 1) { pat = @"^0"; rg=new Regex(pat); mh=rg.Match(textBox1.Text); if(mh.Success) return; MessageBox.Show("不许输入 0.1--0.9 以外的字符"); this.textBox1.Text =""; } else if(this.textBox1.Text.Length == 2) { pat = @"^0\."; rg=new Regex(pat); mh=rg.Match(textBox1.Text); if(mh.Success) return; MessageBox.Show("不许输入 0.1--0.9 以外的字符"); this.textBox1.Text =""; } else if (this.textBox1.Text.Length > 2) { pat = @"^0.[1-9]$"; rg=new Regex(pat); mh=rg.Match(textBox1.Text); //if( !rg.IsMatch(this.textBox1.Text)) if(mh.Success) return; MessageBox.Show("不许输入 0.1--0.9 以外的字符"); this.textBox1.Text =""; } }//测试通过了 多谢大家的指点 问题解决了 小弟用了下面的方法:private void textBox2_KeyPress(object sender, KeyPressEventArgs e) { if (textBox2.Text.Length < 3) { if (textBox2.SelectionStart == 0) { if (e.KeyChar.CompareTo('0') < 0 || e.KeyChar.CompareTo('0') > 0) { e.Handled = true; } } else if (textBox2.SelectionStart == 1) { if (e.KeyChar.CompareTo('.') < 0 || e.KeyChar.CompareTo('.') > 0) { e.Handled = true; } } else if (textBox2.SelectionStart == 2) { if (e.KeyChar.CompareTo('1') < 0 || e.KeyChar.CompareTo('9') > 0) { e.Handled = true; } } } else { if (e.KeyChar != '\b') { e.Handled = true; textBox2.Text = ""; } } } c# Excel导入 问题求解 bindingNavigator上的按钮如何同时显示图片和文字 C#捕捉锁屏 一个程序进一步改进遇到的问题 对V2005 .net 的批评 有winform的form1里的text1和text2, 如何绑定它们,才能将text1.text存到表aaa的1行1列,text2.text存到表aaa的2行1列,它俩是同一字段的 C#中的一个很基本的问题 模拟http协议post内容中有&怎么办 急用:怎样把一段文本转换成xmlDocument? 正则求助 怎么读取注册表项的默认键值,近来就有分 能不能在C#中使用XMLHTTP,如果能的话,怎么使用?
{
decimal a=convert.ToDecimal(TextBox1.Text);
if(a<0.1 || a>0.9)
return;
}
catch
{
return;
}
private textBox_textChanged(object sender,System.EventArgs e)
{
for(int i=0;i<textBox1.text.length;i++)
{
if(textBox1.text[i]!=".")
{
if(!Char.IsDigit(textBox1.text[i]))
{
MessageBox.Show("不能输入数字和小数点以外的任何字符","提示");
textBox1.text="";
return;
}
}
}
}
在textBox的validating事件中检测输入值的大小:
private void textBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
if(textBox.text!="")
{
double inputValue=Convert.toDouble(textBox.text);
if(inputValue>0.9 || inputValue<0.1)
{
MessageBox.Show("输入数字应在0.1到0.9之间,请重新输入!","提示");
textBox.SelectAll();
return;
}
}
}
为了使当输入值不符范围时,仍能正常关闭窗体,请将窗体的causeValidation属性设置为false
判断当前输入是否合法,如果不合法,返回焦点进行修改
if( !reg.IsMatch(this.TextBox1.Text))
this.TextBox1.Text = string.Empty;
private void textBox1_TextChanged_1(object sender, System.EventArgs e)
{// using System.Text.RegularExpressions;
string pat = string.Empty;
Regex rg = null;
Match mh = null; if(this.textBox1.Text.Length == 1)
{
pat = @"^0";
rg=new Regex(pat);
mh=rg.Match(textBox1.Text);
if(mh.Success)
return;
MessageBox.Show("不许输入 0.1--0.9 以外的字符");
this.textBox1.Text =""; }
else if(this.textBox1.Text.Length == 2)
{
pat = @"^0\.";
rg=new Regex(pat);
mh=rg.Match(textBox1.Text);
if(mh.Success)
return;
MessageBox.Show("不许输入 0.1--0.9 以外的字符");
this.textBox1.Text ="";
}
else if (this.textBox1.Text.Length > 2)
{
pat = @"^0.[1-9]$";
rg=new Regex(pat);
mh=rg.Match(textBox1.Text); //if( !rg.IsMatch(this.textBox1.Text)) if(mh.Success)
return;
MessageBox.Show("不许输入 0.1--0.9 以外的字符");
this.textBox1.Text ="";
}
}//测试通过了
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (textBox2.Text.Length < 3)
{
if (textBox2.SelectionStart == 0)
{
if (e.KeyChar.CompareTo('0') < 0 || e.KeyChar.CompareTo('0') > 0)
{ e.Handled = true; }
}
else if (textBox2.SelectionStart == 1)
{
if (e.KeyChar.CompareTo('.') < 0 || e.KeyChar.CompareTo('.') > 0)
{ e.Handled = true; }
} else if (textBox2.SelectionStart == 2)
{
if (e.KeyChar.CompareTo('1') < 0 || e.KeyChar.CompareTo('9') > 0)
{ e.Handled = true; }
}
}
else
{
if (e.KeyChar != '\b')
{ e.Handled = true;
textBox2.Text = "";
}
}
}