//在构造函数里订阅这个事件
this.richTextBox1.KeyPress += new KeyPressEventHandler(richTextBox1_KeyPress);
//在窗口类中增加这个事件实体
void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
//只输入数字
//string str = @"[0-9]";
//只输入小写字母
//string str = @"[a-z]";
//只输入大写字母
//string str = @"[A-Z]";
//只输入小写字母+大写字母+数字
string str = @"[a-zA-Z0-9]";
e.Handled = !System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), str);
}
//这样就可以了~用了正则表达式
c# 如何怎让richtextbox中只能输入数字
最新推荐文章于 2022-11-25 19:17:01 发布