如何实现TextBox中只能输入数字(0~9)

fengyv 2003-10-19 10:46:36
同标题
...全文
920 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
test234 2003-12-16
  • 打赏
  • 举报
回复
up
hunter4500 2003-10-25
  • 打赏
  • 举报
回复
up
fengyv 2003-10-22
  • 打赏
  • 举报
回复
谢谢,收益非浅,搞定了
dragon2002 2003-10-19
  • 打赏
  • 举报
回复
用正则表达 式 查查msdn {d}
xghost 2003-10-19
  • 打赏
  • 举报
回复
mk
zat1978 2003-10-19
  • 打赏
  • 举报
回复
[\d]{1,}
citylamp 2003-10-19
  • 打赏
  • 举报
回复
正则表达式就可以(vb.net改的,不知语法对不)
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
{If Regex.IsMatch(TextBox3.Text, "^\d*$") Then
MsgBox("OK!!");
Else
TextBox3.Text = "";

Stevetan81 2003-10-19
  • 打赏
  • 举报
回复
用 验证控件
JohnnyDJ 2003-10-19
  • 打赏
  • 举报
回复
用一正则表达式就可以了
[0-9]{1,}
xiaha3 2003-10-19
  • 打赏
  • 举报
回复
以下是只能输入decimal类型的类,如果只要0-9,可以设置小数部分的个数为0.不怕粘贴,汉字

using System;
using System.ComponentModel;
using System.ComponentModel .Design ;

namespace contr
{
/// <summary>
///
/// </summary>
public class TextBoxNum : System.Windows.Forms.TextBox
{
public delegate void ValueChangedHandler(object o,decimal DecValue);
public event ValueChangedHandler ValueChanged;
public int MaxInt=8;
public int MaxLittle=2;
public decimal DecimalValue=0;
public TextBoxNum()
{

//
// TODO: 在此处添加构造函数逻辑
//
}
private void InitializeComponent()
{
}
protected override void OnTextChanged(EventArgs e)
{
if(this.Text .Length !=0&&(this.Text[0])=='.')
{
this.Text ="0.";
this.SelectionStart=(this.Text .Length );
return;
}
string s=this.Text.Trim ();
if(this.MaxLittle ==0)
{
if(s.IndexOf ('.')>0||this.Text .Length >this.MaxInt )
{
this.Text =this.ValueDecimal .ToString ();
this.SelectionStart=(this.Text .Length );
return;

}
}

if(s ==""){return;}
int j=s .IndexOf ('.');
if((j<0&&s.Length >this.MaxInt ))
{

this.Text =this.ValueDecimal .ToString ();
this.SelectionStart=(this.Text .Length );
return;
}
if(j>-1&&(s.Length -j-1)>this.MaxLittle)
{
this.Text =this.ValueDecimal .ToString ();
this.SelectionStart=(this.Text .Length );
return;
}
int a=0;

char[] temp=s.ToCharArray () ;
for(int i=0;i<s.Length ;i++)
{
if(Char.IsDigit(temp[i]))
{
continue;
}
else if(temp[i]=='.')
{
a++;
if(a>1)
{
this.Text =this.ValueDecimal .ToString ();
this.SelectionStart=(this.Text .Length );
return;
}

continue;
}
this.Text =this.ValueDecimal .ToString ();
this.SelectionStart=(this.Text .Length );
return;

}
this.ValueDecimal =decimal.Parse (this.Text);
//TextChanged (e);
}

public event ValueChangedHandler DecmalValueChanged
{
add
{
this.ValueChanged += value;
}
remove
{
this.ValueChanged -= value;
}
}
public decimal ValueDecimal
{
get
{
return this.DecimalValue ;
}
set
{
this.DecimalValue =value;
if(ValueChanged!=null)
{
ValueChanged(this,this.DecimalValue );
}

}
}
[Browsable(true),DefaultValue(8)]
public int MaxOfInt
{
get
{
return this.MaxInt ;
}
set
{
if(value<=29)
{
this.MaxInt =value;
this.MaxLength =this.MaxLittle +value +1;
}
else
{
this.MaxInt =29;
this.MaxLength =this.MaxLittle +29 +1;
}
}
}
[Browsable(true),DefaultValue(2)]
public int MaxOfLittle
{
get
{
return this.MaxLittle ;
}
set
{
if(value<=29)
{
this.MaxLittle =value;
this.MaxLength =value +this.MaxInt +1;
}
else
{
this.MaxLittle =29;
this.MaxLength =29 +this.MaxInt +1;

}
}

}
}
}



bencalie 2003-10-19
  • 打赏
  • 举报
回复
private void KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8))
e.Handled = true;
base.OnKeyPress(e);
}
wideroad 2003-10-19
  • 打赏
  • 举报
回复
你要是在winform程序中,在keydown事件中判断刚输入的字符是不是数字就行,在webform中有个验证控件,能验证输入的是否为数字
kuangren 2003-10-19
  • 打赏
  • 举报
回复
方法上面也说了很多了
1可以用ascii判断,
2也可以用char.IsDigit(),IsNum()等等来判断,
3也可以通过try{}catch{}把字符转换为数字来判断,如果产生异常那么说明你输入的不是数字了
方法多的是,还有其他的方法,至于其他的,你可以自己探究
wincore 2003-10-19
  • 打赏
  • 举报
回复
string str="1233sdfsdf";

char myChars[str.length] = str.toCharArray();

foreach(char ch in myChars)
{
if(ch.IsDigit())
.....
else
.....
}
zsww 2003-10-19
  • 打赏
  • 举报
回复
使用RequiredFieldValidator控件
结合正则表达式!!!

-------努力学习 不断实践 虚心讨教--------
rgbcn 2003-10-19
  • 打赏
  • 举报
回复
最简单的方法:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not (IsNumeric(e.KeyChar) Or e.KeyChar = Microsoft.VisualBasic.ChrW(8) Or e.KeyChar = Microsoft.VisualBasic.ChrW(46)) Then
e.Handled = True
End If
End Sub
sendltd 2003-10-19
  • 打赏
  • 举报
回复
知道了,就像玩mud时,用zmud编写机器人一样。呵呵
using System.Text.RegularExpressions;
private void button1_Click(object sender, System.EventArgs e)
{
Regex digitregex = new Regex("(^[0-9]$)");
if (digitregex.IsMatch(textBox1.Text))
{
MessageBox.Show("OK");
}
else
{
MessageBox.Show("PLZ Input number[0-9]");
}
}
delphiseabird 2003-10-19
  • 打赏
  • 举报
回复
正则表达式
[0-9]{1,}
sendltd 2003-10-19
  • 打赏
  • 举报
回复
谁讲讲正则表达式?怎么用?“用正则表达式,^0-9 匹配的replace就OK”何意?
sendltd 2003-10-19
  • 打赏
  • 举报
回复
citylamp(路灯) 的表达式不对,“Regex.IsMatch(TextBox3.Text, "^\d*$")”
c#不认
michaelowenii(少年狂) 的方法不能防止非数字的粘贴
加载更多回复(3)

110,502

社区成员

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

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

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