在线急等!! 解决文本框输入数字而不输入字符的问题!!

aini2046 2009-02-28 07:04:26
怎么判断文本框中输入的字符串是一个在0-99之间的数字?
而当输入字符时让其不能输入呢?有什么比较简单的方法!
...全文
126 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
icehawk 2009-03-01
  • 打赏
  • 举报
回复
注意,输入不仅仅是key,还有可以能是拷贝粘贴
chenxaol 2009-03-01
  • 打赏
  • 举报
回复
在输入的时候加个判断就是的哦、
如果不是数字就清空,
tansoft 2009-02-28
  • 打赏
  • 举报
回复
1.用正則表達式
2.如果不会的话.VS自带的Regularexpress这个验证控件.
比如它的属性里表达就可能选第二个:french postal code:\d{5}五位整数.
whowhen21 2009-02-28
  • 打赏
  • 举报
回复

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("请输入数字!");
}
PandaIT 2009-02-28
  • 打赏
  • 举报
回复
/// <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;
}
}


收藏的一个东西
  • 打赏
  • 举报
回复
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判断会吧
qsd12n 2009-02-28
  • 打赏
  • 举报
回复
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;
}
aini2046 2009-02-28
  • 打赏
  • 举报
回复
但我没有接触过正则表达式,不知道该怎么用啊,有没有简单点的方法呢
gisyellow 2009-02-28
  • 打赏
  • 举报
回复
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("只能输入数字!");
}
}
icehawk 2009-02-28
  • 打赏
  • 举报
回复
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;
}
}
gisyellow 2009-02-28
  • 打赏
  • 举报
回复
使用正则表达式啊,在KeyPress事件中进行判断
正则表达式为 [1-9]?[0-9]

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧