这是我在vb6中编写的代码...它在text1.setfocus中显示错误
Private Sub Text1_Lostfocus()
s1 = Text1.Text
flag = 0
If Text1.Text = "" Then
flag = 1
End If
For i = 1 To Len(s1)
l = Mid(s1, i, 1)
If IsNumeric(l) = True Then
flag = 1
Exit For
End If
Next i
If flag = 1 Then
MsgBox "Enter valid input"
Text1.ForeColor = vbRed
Text1.SetFocus
End If
End Sub
答案 0 :(得分:1)
在LostFocus中没有此代码,而是尝试在Validate事件中使用它,如果设置Cancel = True,则该事件将有一个cancel参数(意味着光标将不会退出控件)做setfocus
答案 1 :(得分:0)
尝试以下操作:
Private Sub Text1_Validate(Cancel As Boolean)
If IsNumeric(Text1.Text) = False Then
MsgBox "Enter valid input"
Text1.ForeColor = vbRed
Cancel = True
End If
End Sub
答案 2 :(得分:0)
如果您有一个空字符串,并且尝试在循环中使用in,请尝试此操作,您将获得无效的过程调用。跳过所有内容,如果文本为空,则不要运行循环。
Private Sub Text1_Lostfocus()
s1 = Text1.Text
flag = 0
If Text1.Text = "" Then
flag = 1
else
For i = 1 To Len(s1)
l = Mid(s1, i, 1)
If IsNumeric(l) = True Then
flag = 1
Exit For
End If
Next i
endif
If flag = 1 Then
MsgBox "Enter valid input"
Text1.ForeColor = vbRed
Text1.SetFocus
End If
结束子