Search code examples
vbaeventsuserform

VBA raising events from modeless Userfrom


I'm trying to raise events from a modeless userform. My starting point is this excellent example. When I show the form modeless, the code raising the event executes, but the event handler never runs (I don't get the expected MsgBox when the Cancel button is clicked.) When I show the form modal, the events are handled as desired, but the form is no longer modeless as desired.

The userform named FormWithEvents has an OKButton and a CancelButton; here's the code behind:

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

Here's the code for the Presenter class that shows the form:

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

And here's the code in the main module:

Option Explicit

Public Sub RunForm()
    With New Presenter
        .Show
    End With
End Sub

Any ideas on where I've gone wrong?


Solution

  • Maybe this instead so you can keep your Presenter instance in scope:

    Dim pres as Presenter
    
    Public Sub RunForm()
        Set pres =  New Presenter
        pres.Show
    End Sub