方法1.
设置ime为disable,用于屏蔽输入法。
private void txtUserId_KeyPress(object sender, KeyPressEventArgs e)
{
if (!(char.IsDigit(e.KeyChar) || char.IsLetter(e.KeyChar)))
e.Handled = true;
//'\b'是退格键值
if (e.KeyChar == '\b')
e.Handled = false;
}
方法2.
wince编程:
private string tempUserId;
private void textBox1_TextChanged(object sender, EventArgs e)
{
// 匹配正则表达式
System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(this.textBox1.Text, @"^[0-9a-zA-Z]*$");
if (!m.Success)
{
this.textBox1.Text = tempUserId;
// 将光标定位到文本框的最后
this.textBox1.SelectionStart = this.textBox1.Text.Length;
}
else
{
tempUserId = this.textBox1.Text;
}
}