第1个回答 2009-09-07
在textBox1中的TextChanged事件里添加如下代码
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
if (this.textBox1.Text.Length>0)
{
if (this.textBox1.Text.Length==1)
{
if (this.textBox1.Text == ",")
{
this.textBox1.Text = "";
this.textBox1.Focus();
return;//首字是 , 时返回
}
else
{
try
{
Int32.Parse(this.textBox1.Text);
}
catch
{
this.textBox1.Text = "";
this.textBox1.Focus();
return;//首字非数字时返回
}
}
}
else
{
try
{
Int32.Parse(this.textBox1.Text.Replace(",",""));
}
catch
{
this.textBox1.Text = "";
this.textBox1.Focus();
return;//替换掉 , 后字符串转换成数字失败时返回
}
}
}
}