VBA 从无模式用户窗体引发事件
VBA raising events from modeless Userfrom
我正在尝试从无模式用户窗体引发事件。我的出发点是。当我显示表单 modeless 时,引发事件的代码执行,但事件处理程序从未运行(单击取消按钮时我没有得到预期的 MsgBox。)当我显示表单 modal,事件按需要处理,但表单不再按需要无模式。
名为 FormWithEvents
的用户表单有一个 OKButton
和一个 CancelButton
;这是背后的代码:
Option Explicit
Public Event FormConfirmed()
Public Event FormCancelled(ByRef Cancel As Boolean)
Private Function OnCancel() As Boolean
Dim cancelCancellation As Boolean
RaiseEvent FormCancelled(cancelCancellation)
If Not cancelCancellation Then Me.Hide
OnCancel = cancelCancellation
End Function
Private Sub CancelButton_Click()
OnCancel
End Sub
Private Sub OKButton_Click()
Me.Hide
RaiseEvent FormConfirmed
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
Cancel = Not OnCancel
End If
End Sub
这是 Presenter
class 的代码,它显示了以下形式:
Option Explicit
Private WithEvents myModelessForm As FormWithEvents
Public Sub Show()
Set myModelessForm = New FormWithEvents
' COMMENT OUT ONE OF THE FOLLOWING TWO LINES TO TOGGLE MODELESS OR MODAL
myModelessForm.Show vbModeless ' Modeless, but events don't get handled (no msgbox on clicking cancel button)
' myModelessForm.Show vbModal ' Events get handled, but no longer modal
End Sub
Private Sub myModelessForm_FormCancelled(Cancel As Boolean)
' Setting cancel to True will leave the form open
Cancel = MsgBox("Cancel this operation?", vbYesNo + vbExclamation) = vbNo
If Not Cancel Then
' Modeless form was cancelled and is now hidden
' ...
Set myModelessForm = Nothing
End If
End Sub
Private Sub myModelessForm_FormConfirmed()
' Form was okayed and is now hidden
Set myModelessForm = Nothing
End Sub
这是主模块中的代码:
Option Explicit
Public Sub RunForm()
With New Presenter
.Show
End With
End Sub
关于我哪里出错的任何想法?
也许可以这样,这样您就可以将 Presenter
实例保留在范围内:
Dim pres as Presenter
Public Sub RunForm()
Set pres = New Presenter
pres.Show
End Sub
避免使用 RaiseEvent
。在标准模块中声明 Presenter
class 的 public 实例。
选项显式
Public Preter As New Presenter
Public Sub RunForm()
With New Presenter
.Show
End With
End Sub
将事件过程转换为具有 public 范围的子过程。
Sub FormCancelled(Cancel As Boolean)
' Setting cancel to True will leave the form open
Cancel = MsgBox("Cancel this operation?", vbYesNo + vbExclamation) = vbNo
If Not Cancel Then
' Modeless form was cancelled and is now hidden
' ...
Set myModelessForm = Nothing
End If
End Sub
在class.
内调用子
Private Function OnCancel() As Boolean
Dim cancelCancellation As Boolean
Preter.FormCancelled cancelCancellation
If Not cancelCancellation Then Me.Hide
OnCancel = cancelCancellation
End Function
我不确定,但是 class 模块中的代码行 Set myModelessForm = Nothing
打扰了我。我想知道是否应该隐藏表单,以便代码可以继续到 Show
过程,然后可以将表单放置在该过程中。
我正在尝试从无模式用户窗体引发事件。我的出发点是
名为 FormWithEvents
的用户表单有一个 OKButton
和一个 CancelButton
;这是背后的代码:
Option Explicit
Public Event FormConfirmed()
Public Event FormCancelled(ByRef Cancel As Boolean)
Private Function OnCancel() As Boolean
Dim cancelCancellation As Boolean
RaiseEvent FormCancelled(cancelCancellation)
If Not cancelCancellation Then Me.Hide
OnCancel = cancelCancellation
End Function
Private Sub CancelButton_Click()
OnCancel
End Sub
Private Sub OKButton_Click()
Me.Hide
RaiseEvent FormConfirmed
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
Cancel = Not OnCancel
End If
End Sub
这是 Presenter
class 的代码,它显示了以下形式:
Option Explicit
Private WithEvents myModelessForm As FormWithEvents
Public Sub Show()
Set myModelessForm = New FormWithEvents
' COMMENT OUT ONE OF THE FOLLOWING TWO LINES TO TOGGLE MODELESS OR MODAL
myModelessForm.Show vbModeless ' Modeless, but events don't get handled (no msgbox on clicking cancel button)
' myModelessForm.Show vbModal ' Events get handled, but no longer modal
End Sub
Private Sub myModelessForm_FormCancelled(Cancel As Boolean)
' Setting cancel to True will leave the form open
Cancel = MsgBox("Cancel this operation?", vbYesNo + vbExclamation) = vbNo
If Not Cancel Then
' Modeless form was cancelled and is now hidden
' ...
Set myModelessForm = Nothing
End If
End Sub
Private Sub myModelessForm_FormConfirmed()
' Form was okayed and is now hidden
Set myModelessForm = Nothing
End Sub
这是主模块中的代码:
Option Explicit
Public Sub RunForm()
With New Presenter
.Show
End With
End Sub
关于我哪里出错的任何想法?
也许可以这样,这样您就可以将 Presenter
实例保留在范围内:
Dim pres as Presenter
Public Sub RunForm()
Set pres = New Presenter
pres.Show
End Sub
避免使用 RaiseEvent
。在标准模块中声明 Presenter
class 的 public 实例。
选项显式
Public Preter As New Presenter
Public Sub RunForm()
With New Presenter
.Show
End With
End Sub
将事件过程转换为具有 public 范围的子过程。
Sub FormCancelled(Cancel As Boolean)
' Setting cancel to True will leave the form open
Cancel = MsgBox("Cancel this operation?", vbYesNo + vbExclamation) = vbNo
If Not Cancel Then
' Modeless form was cancelled and is now hidden
' ...
Set myModelessForm = Nothing
End If
End Sub
在class.
内调用子Private Function OnCancel() As Boolean
Dim cancelCancellation As Boolean
Preter.FormCancelled cancelCancellation
If Not cancelCancellation Then Me.Hide
OnCancel = cancelCancellation
End Function
我不确定,但是 class 模块中的代码行 Set myModelessForm = Nothing
打扰了我。我想知道是否应该隐藏表单,以便代码可以继续到 Show
过程,然后可以将表单放置在该过程中。