读书人

textbox输入限定值的有关问题

发布时间: 2012-01-16 23:36:51 作者: rapoo

textbox输入限定值的问题
我有一个textbox要用来输入身份证号码,如何限定只能输入数字和字母X呢?

[解决办法]
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (textBox1.SelectionLength == 0)
{
e.Handled = (e.KeyChar < '0 ' || e.KeyChar > '9 ' || textBox1.Text.Length > 17);
if (e.KeyChar == (Char)Keys.Back || e.KeyChar == 'X ')
{
e.Handled = false;
}
}
}

读书人网 >C#

热点推荐