-1

这是我在 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
4

3 回答 3

1

在 LostFocus 中不要有此代码,而是尝试在 Validate 事件中使用它,该事件会有一个取消参数,如果设置 Cancel = True (意味着光标不会退出控件),则无需执行 setfocus

于 2019-09-18T22:31:38.937 回答
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
于 2019-09-18T17:24:42.807 回答
0

如果您有一个空字符串并且您尝试在循环中使用,请尝试此操作,您可能会得到一个无效的过程调用。如果文本为空,请跳过所有内容,不要运行循环。

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

结束子

于 2019-10-06T20:05:37.050 回答