两个文本框,TABINDEX属性分别为1和2,以下语句,为防止出现空值,可是当TEXT1获得焦
点时如果不输入任何值按TAB键,后将出现死循环,可是我就是想实现当TEXT1为空时
TEXT1获得焦点而不把焦点转移到下一个控件怎么办呢?
Private Sub Text1_LostFocus()
If Text1.Text = "" Then
   MsgBox "text1不能为空!"
   Text1.SetFocus
End If
End SubPrivate Sub Text2_LostFocus()
If Text2.Text = "" Then
   MsgBox "text2不能为空!"
   Text2.SetFocus
End If
End Sub

解决方案 »

  1.   

    Private Sub Text1_Validate(Cancel As Boolean)
     If Text1.Text = "" Then Cancel = True
    End Sub
      

  2.   

    1,设个模块级变量
    private mbErr as booleanPrivate Sub Text1_LostFocus()
    if mberr then exit sub
    If Text1.Text = "" Then
       mberr = true
       MsgBox "text1不能为空!"
       Text1.SetFocus
    else
       mberr = false
    End If
    End SubPrivate Sub Text2_LostFocus()
    if mberr then exit sub
    If Text2.Text = "" Then
       mberr = true
       MsgBox "text2不能为空!"
       Text2.SetFocus
    else
       mberr = false
    End IfEnd Sub
      

  3.   

    试试这个行不Private Sub Text1_LostFocus()
    If Text1.Text = "" and not text2.text="" Then
       MsgBox "text1不能为空!"
       Text1.SetFocus
    End If
    End SubPrivate Sub Text2_LostFocus()
    If Text2.Text = "" and not text1.text="" Then
       MsgBox "text2不能为空!"
       Text2.SetFocus
    End If
    End Sub
      

  4.   

    我靠,居然会出现死循环,没道理呀VB代码计算器 3.7.4 欢迎下载试用下载地址:
    华军软件 http://www.onlinedown.net/soft/7545.htm
      

  5.   

    Private Sub Text1_Validate(Cancel As Boolean)
     If Text1.Text = "" Then Cancel = True
    End Sub
      

  6.   

    Private Sub Text1_LostFocus()
        If Text1.Text = "" Then
            MsgBox "text不可以为空"
        End If
            Text1.SetFocus
    End Sub
    就这样也可以啊
      

  7.   

    Private Sub Text1_Validate(Cancel As Boolean)
        If Text1.Text = "" Then Cancel = True
    End Sub
    'OK啦。