111,012
社区成员
发帖
与我相关
我的任务
分享
'判断是否为数字及字母
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If (e.KeyChar < Chr(48) Or e.KeyChar > Chr(57)) And _
e.KeyChar <> Chr(8) And e.KeyChar <> Chr(13) And e.KeyChar <> Chr(9) And _
(e.KeyChar > Chr(90) Or e.KeyChar < Chr(65)) And _
(e.KeyChar > Chr(122) Or e.KeyChar < Chr(97)) Then
e.KeyChar = Chr(0) '不是为大、小写字母及数字则为空
End If
End Sub
'设置长度及输入转换为大写
Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter
TextBox1.MaxLength = 4
TextBox1.CharacterCasing = CharacterCasing.Upper
End Sub
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
int textboxLenth = 4; //数据长度
bool isIntLetter = (e.KeyChar > 47 && e.KeyChar < 58) || (e.KeyChar > 64 && e.KeyChar < 91) || (e.KeyChar > 96 && e.KeyChar < 123);
//48-57 ASCII--> 0-9 65-90-->A-Z 97-122-->a-z ASCII 8 退格键
if (!isIntLetter && e.KeyChar != 8)
{
e.Handled = true;
}
else if (textBox1.Text.Trim().Length >= textboxLenth && e.KeyChar != 8)
{
e.Handled = true;
}
else if (e.KeyChar > 96 && e.KeyChar < 123)
{
e.KeyChar =(char)(e.KeyChar- 32);
}
}