VB中判断text不能为空

来源:百度知道 编辑:UC知道 时间:2024/06/27 22:21:42
本人菜鸟,初来乍到,有个小问题问下:
在VB中如何判断text1中不能为空,如按回车、或Tab、或鼠标点击到下一个text2中时弹出提示框,且光标无法进入text2,回到text1中。

在其他地方找到的代码:
if Text1.Text="" then MsgBox "内容为空"
这个代码按确定后可以进入下一项,没办法实现我要的功能。

如果在多个文本中应用为空验证 应该加一个开关 以免产生多个同时验证

Dim tState As Boolean '开关

Private Sub Text1_GotFocus()'获取焦点时
tState = True
End Sub

Private Sub Text2_GotFocus()'获取焦点时
tState = True
End Sub

Private Sub Text1_LostFocus()'失去焦点时
If Text1.Text = "" And tState Then
MsgBox "该项不能为空"
Text1.SetFocus
End If
tState = False
End Sub

Private Sub Text2_LostFocus()
If Text2.Text = "" And tState Then
MsgBox "该项不能为空"
Text2.SetFocus
End If
tState = False
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
If Text1 = "" Then MsgBox "不能为空!": Text1.SetFocus
End If
End Sub

Private Sub Text1_LostFocus()
If Text1 = "" Then MsgBox "不能为空!": Text1.SetFocus
End Sub

Private Sub Text1_LostFocus()
If Len