C# 禁止汉字输入
注意:在C#中,不论中英文,均要占两个字节,原因是C#采用的Unicode编码
System.Text.StringBuilder s = new System.Text.StringBuilder(0xFFFF * 3);
for (int i = 128; i < 0xFFFF; i++)
{
if (i % 50 == 0)
{
s.Append("\r\n");
}
s.AppendFormat("{0,3}", (char)i);//长度为3
}
textBox1.Text = s.ToString();
//起始汉字为"一",最后一个汉字为"龥"
//获得他们的Unicode(十六进制)4e00,9fa5
textBox1.Text = string.Format("{0:x},{1:x}", (int)'一', (int)'龥');
//禁止汉字输入
if((e.KeyChar >=(char)0x4e00) && (e.KeyChar <= (char)0x9fa5))
{
e.Handled = true;
}
//判断是字符还是汉字
char c = e.KeyChar;
if (Char.IsDigit(c))
{
MessageBox.Show("数字");
}
else if (Char.IsLetter(c))
{
MessageBox.Show("字母");
}
else
{
MessageBox.Show("其他");
}
//判断是否是汉字
string s = this.textBox2.Text;
foreach (char c in s)
{
int i = (int)c;
if(i<0x4e00 || i>0x9fa5)
{
MessageBox.Show ("不是汉字");
}
}

浙公网安备 33010602011771号