'解决方案 Option ExplicitPrivate Type POINTAPI X As Long Y As Long End TypePrivate Type RECT Left As Long Top As Long Right As Long Bottom As Long End TypePrivate Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As LongPrivate Sub Command1_Click() Unload Me End SubPrivate Sub Text1_LostFocus() Call CheckInput End SubPrivate Sub CheckInput() Dim Fhwnd As Long Dim pos As POINTAPI GetCursorPos pos Fhwnd = WindowFromPoint(pos.X, pos.Y) Debug.Print "me.hwnd=" & Me.hWnd & "---Command1.hwnd=" & Command1.hWnd & "---text1.hwnd=" & Text1.hWnd & "---posHwnd=" & Fhwnd If Fhwnd = Command1.hWnd Then Unload Me Exit Sub End If If Trim(Text1.Text) = "" Then MsgBox "请输入数据" Text1.SetFocus End If End Sub
将cmdExit的CausesValidation属性设为False;Validate事件在控件失去焦点之前发生,而且仅当即将接收焦点的控件的CausesValidation属性为True时才发生。上面的处理方法将在点击cmdExit时忽略掉数据验证代码,也就不会出现对话框了。
Option ExplicitPrivate Type POINTAPI
X As Long
Y As Long
End TypePrivate Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End TypePrivate Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As LongPrivate Sub Command1_Click()
Unload Me
End SubPrivate Sub Text1_LostFocus()
Call CheckInput
End SubPrivate Sub CheckInput()
Dim Fhwnd As Long
Dim pos As POINTAPI
GetCursorPos pos
Fhwnd = WindowFromPoint(pos.X, pos.Y)
Debug.Print "me.hwnd=" & Me.hWnd & "---Command1.hwnd=" & Command1.hWnd & "---text1.hwnd=" & Text1.hWnd & "---posHwnd=" & Fhwnd
If Fhwnd = Command1.hWnd Then
Unload Me
Exit Sub
End If
If Trim(Text1.Text) = "" Then
MsgBox "请输入数据"
Text1.SetFocus
End If
End Sub
在Text1的Validate事件中进行数据验证;
将cmdExit的CausesValidation属性设为False;这个可以,我试过了!
我想示例代码应该是解决问题的最好方法,所以我想先试一下“为你伤心”这位看官的方法。
end sub
endif