上次发了一个贴子,问了这个问题,当时没有加变量,当text1或者text2的内容为空的时候并触发lostfocus事件后无法终止(相当于死循环)!
后来一个朋友提示我加一个全局变量。现在又有问题出现了。又要麻烦大家了。谢谢!
我加了一个全局变量,在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

解决方案 »

  1.   

    试试Validate事件,专门用做验证的:
    Private Sub Text1_Validate(Cancel As Boolean)
        If Text1.Text = "" Then
            Call MsgBox("请输入数据")
            Cancel = True
        End If
    End Sub
      

  2.   

    Private Sub Text1_LostFocus()
        If TextValidate = False Then
            Exit Sub
        End If
    End SubPrivate Sub Text2_LostFocus()
        If Text2.Text = "" Then
            Exit Sub
        End If
    End SubPublic Function TextValidate()
        
        If Text1.Text = "" Then
            TextValidate = False
            MsgBox "text1 kong"
            Text1.SetFocus
            Exit Function
        End If
        
        If Text2.Text = "" Then
            TextValidate = False
            MsgBox "text2 kong"
            Text2.SetFocus
            Exit Function
        End If
        
        TextValidate = True
        
    End Function
    这样写也行
    也可以把验证从LostFocus事件中移到提交事件中
      

  3.   

    如果text1与text2都是空的时候,点击命令按钮会提示错误,命令按钮里的代码不会执行。
      

  4.   


    Private Sub Command1_Click() 
        If TextValidate = False Then
            Exit Sub
        End If
        
        '具体执行的代码
         ...
    End Sub text1与text2都是空的时候和这句话放哪里没有关系
    text1为空就已经退出调用的过程了
    后面代码为什么还要执行啊
    如果还要执行 那就是你的具体业务了 那要具体分析了
      

  5.   

    把那个什么全部变量over掉
      
     private function check() '在任何需要检查的地方调用这个函数
        if len(text1.text)=0 and len(text2.text)=0 then
                   check=3
                   exit function
        end if
        if len(text1.text)=0 then
           check=1
            exit function
        end if
        if len(text2.text)=0 then
           check=2
            exit function
        end if
        check=0
     end function
        
        
      

  6.   

    楼主,你的意思是让 Command1 不理会 TextBox 是否为空,继续运行?1 把你的 Textn_LostFocus 换成 Textn_Validate 事件,代码要按 1 楼那样写。
    2 把你不需要触发 Validate 事件的按钮的 CausesValidate 属性设置为 False。
      

  7.   


    对,我的意思就是希望点击Command1按钮时不理会textbox是否为空。麻烦帮忙写一段代码。我按1楼的方法试了好象也不行。
      

  8.   


    谢谢!把command1.causesvalidate属性设置为false就OK了!谢谢!!!!
      

  9.   

    To:  of123command1可以设置causesvalidate属性。
    toolbar上的按钮如何有没有causesvalidate属性设置?
      

  10.   

    Private Sub Text1_GotFocus()
        Text1.SelStart = 0
        Text1.SelLength = Len(Text1)
    End SubPrivate Sub Text1_LostFocus()
        If Me.ActiveControl.Name = Me.Command1.Name Then Exit Sub
        If Len(Text1) = 0 Then
            MsgBox "文本框不能为空"
            Text1.SetFocus
        End If
    End Sub
    '是不是这样的要求呢?
      

  11.   

    谢谢楼上的。不过先前的问题解决了。
    现在如果那个按钮不是command.而是toolbar上的button.应该如何处理
      

  12.   

    加个变量:
    dim ss as stringPrivate Sub Command1_Click() 
    set ss = 1
    Text3 = Text1 & Text2 
    End Sub Private Sub Text1_LostFocus() 
    if ss = 1 then exit sub
    else
    If bIsErr = False And Text1 = "" Then 
    MsgBox "text1 不能为空" 
    bIsErr = True 
    Text1.SetFocus 
    End If 
    end if
    End Sub 
      

  13.   

    我用VB6.0运行了一下可以正常运行啊没有出错在text1获得焦点时,再点击其它按钮时,点击的事件是在运行的比如点command1它就会text3内容就会为text2+texe1的,点command2就会使text1和text2都失效,并且显示没有出错的,是不是你的vb6.0有问题啊
      

  14.   

    TO:25F你是不是在TEXT1和TEXT2都为空的情况下点击command1或者command2的?