一、给文本框加字符限制
1.输入数字和删除键
Private Sub txtSID_KeyPress(KeyAscii As Integer)
If KeyAscii <> 8 And (KeyAscii < 48 Or KeyAscii > 57) Then
KeyAscii = 0
End If
End Sub
2.输入中英文及删除键
Private Sub txtName_KeyPress(KeyAscii As Integer)
If ((KeyAscii <= -3652 And KeyAscii >= -20319) Or (KeyAscii >= 65 And KeyAscii <= 90) Or (KeyAscii >= 97 And KeyAscii <= 122) Or KeyAscii = 32 Or KeyAscii = 8) = False Then
KeyAscii = 0
End If
End Sub
3.如何限制本文字符长度
Private Sub txtAddress_KeyPress(KeyAscii As Integer)
txtAddress.MaxLength = 25
End Sub
4.只能输入数字和英文字母
Private Sub txtPassword1_KeyPress(Index As Integer, KeyAscii As Integer)
If ((KeyAscii >= 48 And KeyAscii <= 57) Or (KeyAscii >= 65 And KeyAscii <= 90) Or (KeyAscii >= 97 And KeyAscii <= 122) Or KeyAscii = 8) = False Then KeyAscii = 0
End Sub
5.日期的判断
Dim Borndate As Date
Dim Rudate As Date
Borndate = Trim(txtBorndate.Value)
Rudate = Trim(txtRudate.Value)
If Rudate <= Borndate Then '进行比较
MsgBox "还未出生就入学了,这么神奇?", vbOKOnly + vbInformation, "警告"
txtRudate.SetFocus
Exit Sub
End If
利用VB中的DTPicker,是慧慧告诉我的,当时我还好郁闷的,想了老半天,怎么才能实现自选日期呢,原来有控件呀!
添加过程:工具→部件→控件→Microsoft Windows common control2.6.0→应用→确定
这个控件与文本框不同之处在于它是用value属性,因为这个当时还不知道怎么回事儿呢,发现在后面加上.后,没有text属性,这才明白了,人家本来就不是文本框呀。
6.登陆次数限定
If miCount = 1 Then
MsgBox "您还有两次机会哦(*^▽^*)", 48, "温馨提示:"
Exit Sub
End If
If miCount = 2 Then
MsgBox "您还有一次机会哦(*^▽^*)", 48, "温馨提示:"
Exit Sub
End If
If miCount = 3 Then
MsgBox "很遗憾,程序即将关闭! ̄へ ̄", 48, "温馨提示:"
End
End If
txtPassword.SetFocus
txtPassword.Text = ""
End If
二、给文本框是否能够编辑加限制
1.空字符,
Private Sub comboClassNo_KeyPress(KeyAscii As Integer)
KeyAscii = 0
End Sub
2.修改信息时,未编辑状态下,按钮不可弄
comboExamtype.Enabled = False
comboClassno.Enabled = False
comboSID.Enabled = False
comboCourse.Enabled = False
txtName.Enabled = False
txtResult.Enabled = False