标签:des style blog color art io
1,限制只能输入数字
private void txtSize_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar != 8 && e.KeyChar != 13 && !char.IsDigit(e.KeyChar) && e.KeyChar != 46) { MessageBox.Show("请输入数字"); e.Handled = true; } }
2,限制只能输入大小写字母
private void txtChar_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar != 8 && e.KeyChar != 13 && (!char.IsUpper(e.KeyChar) && !char.IsLower(e.KeyChar))) { MessageBox.Show("请输入字母。"); e.Handled = true; } }
3,限制输入1位字母
private void txtClassCode_KeyPress(object sender, KeyPressEventArgs e) { if (frmStart != "IsEdit") { if (e.KeyChar != 8 && e.KeyChar != 13 && (!char.IsUpper(e.KeyChar) && !char.IsLower(e.KeyChar))) { MessageBox.Show("请输入字母。"); e.Handled = true; } if (e.KeyChar != 8 && e.KeyChar != 13 && (txtClassCode.Text.Length - txtClassCode.SelectedText.Length > 0)) { MessageBox.Show("只允许输入一位字母。"); e.Handled = true; } } }
4,限制输入内容长度
private void SetTextLen() { this.txtClassCode.MaxLength = 30; this.txtClassName.MaxLength = 100; this.txtNnemonicCode.MaxLength = 128; this.txtSpellCode.MaxLength = 255; this.txtDescription.MaxLength = 255; this.txtRemark.MaxLength = 255; }
然后在窗体默认加载事件中调用此方法。
注:这样设置的文本框输入长度限制,在手动输入情况下是生效的。但是如果是程序赋值给文本框,则长度限制会不起作用。
.Net常用技巧_TextBox文本框限制输入的内容,布布扣,bubuko.com
标签:des style blog color art io
原文地址:http://www.cnblogs.com/yuyuanfeng/p/3811410.html