//使用TextChanged事件
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
string text = ((TextBox)sender).Text;
if(text.Length>0)
{
string newCc = text.Substring(text.Length-1);
char [] chars = newCc.ToCharArray();
bool iscn = false;
foreach(char ch in chars)
{
short st = (short)ch;
if(st>255)
{
iscn = true;
break;
}
}
if(iscn)
MessageBox.Show("是中文");
else
MessageBox.Show("不是中文");
}
}
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
string text = ((TextBox)sender).Text;
if(text.Length>0)
{
string newCc = text.Substring(text.Length-1);
char [] chars = newCc.ToCharArray();
bool iscn = false;
foreach(char ch in chars)
{
short st = (short)ch;
if(st>255)
{
iscn = true;
break;
}
}
if(iscn)
MessageBox.Show("是中文");
else
MessageBox.Show("不是中文");
}
}
{
e.handle = true;
}
/// 限制输入数字,用keydown()。
/// </summary>
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if((e.KeyValue>96&&e.KeyValue<123)||(e.KeyValue>64&&e.KeyValue<91))
{
MessageBox.Show("不允许输入字母,请输入数字!");
if (textBox1.Text!="")
textBox1.Text=textBox1.Text.Substring(0,textBox1.Text.Length-1);
}
}
加一个正则表达式来验证
“\d”验证数字“\.”验证小数点
无穷次匹配就是了
数字的政则表达式应该是这样的 [\d\.]+
很方便,不需要很长的代码
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (!(e.KeyChar >= '0' && e.KeyChar <= '9')) e.Handled = true;
}