我写了对于几个txtbox的判断,如下:
if text1.text="" then
msgbox "text1为空!"
end ifif text2.text="" then
msgbox "text2为空!"
end ifif text3.text="" then
msgbox "text3为空!"
end ifif text4.text="" then
msgbox "text4为空!"
end if
如果我四个txtbox都为空,它会一路的判断下来,我想让它发现第一个为空的,判断了,弹出消息框之后,就把焦点回到这个txtbox上让人进行填写,怎么办呢!
if text1.text="" then
msgbox "text1为空!"
end ifif text2.text="" then
msgbox "text2为空!"
end ifif text3.text="" then
msgbox "text3为空!"
end ifif text4.text="" then
msgbox "text4为空!"
end if
如果我四个txtbox都为空,它会一路的判断下来,我想让它发现第一个为空的,判断了,弹出消息框之后,就把焦点回到这个txtbox上让人进行填写,怎么办呢!
msgbox "text1为空!"
text1.sefocus
exit sub
end if
msgbox "text1为空!"
text1.setfocus
exit sub
end if
If SetTxtFocus(Text1) Then Exit Sub
If SetTxtFocus(Text2) Then Exit Sub
If SetTxtFocus(Text3) Then Exit Sub
If SetTxtFocus(Text4) Then Exit Sub
End SubPublic Function SetTxtFocus(ctl As TextBox) As Boolean
SetTxtFocus = False
If ctl.Text = "" Then
ctl.SetFocus
MsgBox ctl.Name & "为空!"
SetTxtFocus = True
End If
End Function
还有,Function如何调用?
If Text1 = "" Then
Cancel = True
MsgBox "text1为空!"
End If
End SubPrivate Sub Text2_Validate(Cancel As Boolean)
If Text1 = "" Then
Cancel = True
MsgBox "text2为空!"
End If
End Sub......注意将关闭窗体的按钮(不是的控制栏的X,而是你自己放的按钮)的 CauseValidate 属性设置为 False。当然,利用控件数组代码就更简单:
Private Sub Text1_Validate(Index As Integer, Cancel As Boolean)
If Text1(Index) = "" Then
Cancel = True
MsgBox "text" & index & "为空!"
End If
End Sub