c# 怎么让richtextbox中只能输入英文和数字,不能输入中文??

怎么让richtextbox中只能输入英文和数字,不能输入中文??

先谢谢大家了
能说得具体点吗???

英文和数字?那就只能从unicode码来限制了

你的英文和数字包括全角的吗?

触发TextChanged事件
private void textBox1_TextChanged(object sender, EventArgs e)
{
char[] clist = textBox1.Text.ToCharArray();
string newStr = "";
for (int i = 0; i < clist.Length; i++)
{
int ascii = (int)clist[i];
if (ascii > 127)
{
clist[i] = '\0';
}
newStr += clist[i].ToString();
}

textBox1.Text = newStr;
}

之所以用TextChanged而不用Key相关事件,是考虑到还有不通过键盘输入,比如复制进来一段文字的情况
当然如果你允许复制时输入中文,还是用Key事件吧
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-04-02
this.richTextBox1.KeyPress += new KeyPressEventHandler(richTextBox1_KeyPress);
//上面的你在构造函数里订阅这个事件

void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
string str = @"[a-zA-Z0-9]";
e.Handled = !System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), str);

}
//这样就可以了~用了正则表达式
相似回答