我想控制text1内容如果是 "text1" ,那么就提示错误,text1重新得到焦点.
如果text2内容为空,那么提示错误,text2重新得到焦点.我的代码如下:  当text1的内容为"text1"时,就会进入死循环,请问如何解决.Private Sub Text1_LostFocus()
If Text1 = "Text1" Then
MsgBox "错误"
Text1.SetFocus
End If
End SubPrivate Sub Text2_LostFocus()
If Text2 = "" Then
MsgBox "error"
Text2.SetFocus
End If
End Sub

解决方案 »

  1.   

    加个全局变量,
    dim bIsErr as Boolean
    bIsErr = false;Private Sub Text1_LostFocus() 
    If Text1 = "Text1" Then 
        bIsErr = true
        MsgBox "错误" 
    end if
    Text1.SetFocus 
    End If 
    End Sub然后在Text1的change事件中加bIsErr = false;
      

  2.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
       If KeyAscii = 13 Then
          If UCase(Text1.Text) = UCase("Text1") Then
             MsgBox "错误"
             Text1.SetFocus
          Else
             Text2.SetFocus
          End If
          KeyAscii = 0
       End If
    End SubPrivate Sub Text2_KeyPress(KeyAscii As Integer)
       If KeyAscii = 13 Then
          If Trim(Text2.Text) = "" Then
             MsgBox "错误"
             Text2.SetFocus
          Else
             Text1.SetFocus
          End If
          KeyAscii = 0
       End If
    End Sub
      

  3.   

    Private Sub Text1_LostFocus() 
        If Text1 = "Text1" Then 
            MsgBox "错误" 
            text1.text=""      '<----加上这句试试
            Text1.SetFocus 
        End If 
    End Sub 
      

  4.   

    谢谢提醒.
    我加了一个变量解决了问题!我是在text2的lostfocus事件中增加了一个判断来实现的.Dim IsErr As BooleanPrivate Sub Text1_LostFocus()
    If Text1 = "Text1" Then
    IsErr = True
    MsgBox "错误"
    Text1.SetFocus
    Else
    IsErr = False
    End If
    End SubPrivate Sub Text2_LostFocus()
    If IsErr = False And Text2 = Empty Then
    MsgBox "error"
    Text2.SetFocus
    End If
    End Sub
      

  5.   

    Option ExplicitPrivate Sub Text1_Change()
            If Text1.Text = "Text1" Then
               MsgBox "错误"
               Text1.SetFocus
            End If
    End Sub
    Private Sub Text2_Change()
            If Text2.Text = "" Then
               MsgBox "error"
               Text2.SetFocus
            End If
    End Sub
      

  6.   


    这样的话,如果在text1输入内容时,如果想输入的内容是  text111111的话,那么输到一半就会提示错误呀.
    在text2输入内容时,如果输入错了.全部删除后也是提示错误的.
      

  7.   


    P这个只有在按回车键才会发生事件,如果按TAB,如果直接用鼠标操作呢?
      

  8.   

    又遇到麻烦了!我加了一个全局变量,在text1与text2失去焦点的不会死循环了.但是如果在text1获得焦点时,再点击其它按钮时,点击的事件不会运行.提示错误.我的代码如下.
    请问怎么解决!Dim bIsErr As BooleanPrivate Sub Command1_Click()
    Text3 = Text1 & Text2
    End SubPrivate Sub Command2_Click()
    Text1.Enabled = False
    Text2.Enabled = False
    Text3 = "取消"
    End SubPrivate Sub Text1_GotFocus()
    bIsErr = False
    End SubPrivate Sub Text1_LostFocus()
    If bIsErr = False And Text1 = "" Then
    MsgBox "text1 不能为空"
    bIsErr = True
    Text1.SetFocus
    End If
    End SubPrivate Sub Text2_GotFocus()
    bIsErr = True
    End SubPrivate Sub Text2_LostFocus()
    If bIsErr = False And Text2 = "" Then
    MsgBox "text2 不能为空..."
    bIsErr = True
    Text2.SetFocus
    End If
    End Sub