在线急等!! 解决文本框输入数字而不输入字符的问题!! 怎么判断文本框中输入的字符串是一个在0-99之间的数字?而当输入字符时让其不能输入呢?有什么比较简单的方法! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用正则表达式啊,在KeyPress事件中进行判断正则表达式为 [1-9]?[0-9] private string text = "";void TextBox1TextChanged(object sender, EventArgs e){TextBox box = sender as TextBox;string newtxt = box.Text.ToString();System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"^\d{0,2}$");if(!reg.IsMatch(newtxt)){box.Text = text;box.SelectionStart = text.Length;}else{text = newtxt;}} private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { string str = textBox1.Text; int i; if (Int32.TryParse(str, out i)) { if (i < 0 || i > 99) { MessageBox.Show("请输入0~99之间的整数"); } } else { MessageBox.Show("只能输入数字!"); } } public bool isnum(string s) { for (int i = 2; i < s.Length; i++) { if (!char.IsNumber(s, i)) { return false; } } return true; } private void button2_Click(object sender, System.EventArgs e) { if ((!isnum(textBox1.Text)) || textBox1.Text == "") { MessageBox.Show("请输入数字"); return; } if ((e.KeyChar < 48 && e.KeyChar != 8 && e.KeyChar != 16) || e.KeyChar > 57) { e.Handled = true; MessageBox.Show("请输入数字!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtMyQQNum.Text = ""; }另外判断一下长度if判断会吧 /// <summary> /// 屏蔽非数字键 /// </summary> protected override void OnKeyPress(KeyPressEventArgs e) { base.OnKeyPress(e); if (this.ReadOnly) { return; } // 特殊键, 不处理 if ((int)e.KeyChar <= 31) { return; } // 非数字键, 放弃该输入 if (!char.IsDigit(e.KeyChar)) { e.Handled = true; return; } }收藏的一个东西 int getInt = null;try{ int temp = int.Parse(textBox1.Text); if(temp < 0 || temp >99) { MessageBox.Show("请输入大于0 小于 99数字!"); return; } getInt = temp;}catch{ MessageBox.Show("请输入数字!");} 1.用正則表達式2.如果不会的话.VS自带的Regularexpress这个验证控件.比如它的属性里表达就可能选第二个:french postal code:\d{5}五位整数. 注意,输入不仅仅是key,还有可以能是拷贝粘贴 请教个窗体间传递信息和关闭的问题 如何让richtextbox插入多张图片且顺序为从上至下 各位高手,帮我想一个解决办法。在线等。。。。。。。。。 C# 中的Dictionary的索引查找算法,是用对象HashCode+二叉树查法吗? 怎么随机初始化这样的一个二维数组?(想了一星期都没想出来,我都快想疯了) 求救 【求助】怎么把集合里面的数据导入到sqlserver数据库中啊 c#2008问题 请教动态连接库调用的问题 哪位高人懂AnimateWindow的运用? asp.net不同类别的多附件上传问题 [高手帮忙]怎样在windows service中执行一个exe文件
正则表达式为 [1-9]?[0-9]
void TextBox1TextChanged(object sender, EventArgs e)
{
TextBox box = sender as TextBox;
string newtxt = box.Text.ToString();
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"^\d{0,2}$");
if(!reg.IsMatch(newtxt))
{
box.Text = text;
box.SelectionStart = text.Length;
}
else
{
text = newtxt;
}
}
{
string str = textBox1.Text;
int i;
if (Int32.TryParse(str, out i))
{
if (i < 0 || i > 99)
{
MessageBox.Show("请输入0~99之间的整数");
}
}
else
{
MessageBox.Show("只能输入数字!");
}
}
{
for (int i = 2; i < s.Length; i++)
{
if (!char.IsNumber(s, i))
{
return false;
}
}
return true;
} private void button2_Click(object sender, System.EventArgs e)
{
if ((!isnum(textBox1.Text)) || textBox1.Text == "")
{
MessageBox.Show("请输入数字");
return;
}
{
e.Handled = true;
MessageBox.Show("请输入数字!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtMyQQNum.Text = "";
}另外判断一下长度if判断会吧
/// 屏蔽非数字键
/// </summary>
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e); if (this.ReadOnly)
{
return;
}
// 特殊键, 不处理
if ((int)e.KeyChar <= 31)
{
return;
} // 非数字键, 放弃该输入
if (!char.IsDigit(e.KeyChar))
{
e.Handled = true;
return;
}
}收藏的一个东西
int getInt = null;
try
{
int temp = int.Parse(textBox1.Text);
if(temp < 0 || temp >99)
{
MessageBox.Show("请输入大于0 小于 99数字!");
return;
}
getInt = temp;
}
catch
{
MessageBox.Show("请输入数字!");
}
2.如果不会的话.VS自带的Regularexpress这个验证控件.
比如它的属性里表达就可能选第二个:french postal code:\d{5}五位整数.