program by yourself
for(int ii=0;ii<str.Length;ii++)
{
if(str.substr(ii,1) >= "0" && str.substr(ii,1) <="9")
{
}
else
{
}
}2
trystring s = "...";
if (System.Text.RegularExpressions.Regex.Replace(s,"[0-9]","").Length ==0)
{
//字符串只有字符(0~9)组成
}3
string s = "....";
if(!System.Text.RegularExpressions.Regex.IsMatch(s,@"^\d*$"))
{
//字符串只有字符(0~9)组成
}
for(int ii=0;ii<str.Length;ii++)
{
if(str.substr(ii,1) >= "0" && str.substr(ii,1) <="9")
{
}
else
{
}
}2
trystring s = "...";
if (System.Text.RegularExpressions.Regex.Replace(s,"[0-9]","").Length ==0)
{
//字符串只有字符(0~9)组成
}3
string s = "....";
if(!System.Text.RegularExpressions.Regex.IsMatch(s,@"^\d*$"))
{
//字符串只有字符(0~9)组成
}
作textbox的KeyPress中:
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8))
e.Handled = true;
base.OnKeyPress(e);}说明:
e.KeyChar != 8是为了让退格键起作用
限制大下,修改e.KeyChar < 48 || e.KeyChar > 57.2.-----------------------------
If somebody use copy/paste, It also can paste something into the textbox, so and the following code to textchange event.private void textBox1_TextChanged(object sender, System.EventArgs e)
{
if(!System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text,@"^\d*$"))textBox1.Undo();
}