C#的winform中如何控制TextBox中只能输入数字?

sixsir 2007-05-29 08:02:10
问题如题
...全文
5910 52 打赏 收藏 转发到动态 举报
写回复
用AI写文章
52 条回复
切换为时间正序
请发表友善的回复…
发表回复
wy804875978 2012-05-19
  • 打赏
  • 举报
回复
最简单的就是添加KeyPress事件

private void NowTime_KeyPress(object sender, KeyPressEventArgs e)
{
NowTime.ReadOnly = true;
}
gohappy2008 2010-04-27
  • 打赏
  • 举报
回复
给个最简单的方法:
private void textBox3_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
//阻止从键盘输入键
e.Handled = true;
if(e.KeyChar>='0' && e.KeyChar <='9')
{
e.Handled = false;
}

}


多条件的:

private void TxtUser_KeyPress(object sender, KeyPressEventArgs e)
{
//阻止从键盘输入键
e.Handled = true;

if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == (char)8))
{

if ((e.KeyChar == (char)8)) { e.Handled = false; return; }
else
{
int len = TxtUser.Text.Length;
if (len < 5)
{
if (len == 0 && e.KeyChar != '0')
{
e.Handled = false; return;
}
else if(len == 0)
{
MessageBox.Show("编号不能以0开头!"); return;
}
e.Handled = false; return;
}
else
{
MessageBox.Show("编号最多只能输入5位数字!");
}
}
}
else
{
MessageBox.Show("编号只能输入数字!");
}


}


兔子-顾问 2010-04-27
  • 打赏
  • 举报
回复
发表于:2007-05-29 20:02:10
陈年老帖被lnnneu2009翻出来了。OJ,来结贴哦。
qq389900275 2010-04-27
  • 打赏
  • 举报
回复
KeyPress事件:

if (!(Char.IsNumber(e.KeyChar) || e.KeyChar == 8 || e.KeyChar == '.'))
{
e.Handled = true;
}
lanxiaoajn 2010-04-27
  • 打赏
  • 举报
回复
正则表达式貌似有用
ljt_09 2010-04-27
  • 打赏
  • 举报
回复
最主要的是如何处理粘贴的情况!
自由_ 2010-04-27
  • 打赏
  • 举报
回复
学习
学习
Comeonzhou 2010-04-27
  • 打赏
  • 举报
回复
结贴率太高
蹊跷路人 2010-04-27
  • 打赏
  • 举报
回复

/// <summary>
/// 只允许录入数字
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tb_KeyPress(object sender, KeyPressEventArgs e)
{

if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 46)
{
e.Handled = true;
}
else
{
if (e.KeyChar == 46 && tb.Text.IndexOf('.') > -1)
{
e.Handled = true;
}
}
}
dlydlydly 2010-04-27
  • 打赏
  • 举报
回复
private void textbox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}

阿双2009 2010-04-27
  • 打赏
  • 举报
回复

private void textbox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar)) //只允许输入数字键和退格键
{
e.Handled = true;
}
}
daredjever 2010-04-27
  • 打赏
  • 举报
回复
没看都上说下我的想法:
在textchange函数里每次change都把内容转化成数字试试,如果错误就删掉刚才键入的字符
lnnneu2009 2010-04-27
  • 打赏
  • 举报
回复
谢谢lz
langyue555 2010-04-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 asc13 的回复:]
在TextBox的KeyPress事件里判断输入值的ASC码,如果不为数字就把e.Handled设为Ture,取消KeyPress事件
[/Quote]

同意
孤芳-自赏 2009-11-24
  • 打赏
  • 举报
回复
同意
[Quote=引用 29 楼 wx100200 的回复:]
网页里面的话可以用正则表达,很方便,这个我就不清楚怎么弄了
[/Quote]
kinglshadow 2009-11-24
  • 打赏
  • 举报
回复
受益匪浅
hiddkiller 2009-11-24
  • 打赏
  • 举报
回复
呵呵, 鼠标右键粘贴时会绕开KeyPress 事件. 都不知道你们在瞎扯什么.
参考14楼的吧,消息处理.
ourola 2009-11-24
  • 打赏
  • 举报
回复
正则是王道
coolio1 2009-11-24
  • 打赏
  • 举报
回复
我太菜了,只会这个
if(e.KeyChar> = '0 '   &&   e.KeyChar   <= '9 ')
{
e.Handled = false;
}
liuyuainil 2009-11-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 kason_j 的回复:]
KeyPress事件里
[/Quote]
正解
加载更多回复(32)

110,526

社区成员

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

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

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