View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default XL2003: Is it possible to sink a form's events in a class module ?

As you say the QueryClose event is not exposed to WithEvents, nor are all
events of all userform controls.

Not sure why you'd need to sink such an event in a class module though could
do something like this -

'userform
Dim clsFrm As Class1

Private Sub CommandButton1_Click()
Unload Me
End Sub

Private Sub UserForm_Initialize()

Me.Caption = "Test QueryClose"
Set clsFrm = New Class1
Set clsFrm.frm = Me
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, _
CloseMode As Integer)
Cancel = clsFrm.QClose(CloseMode, Me.Caption)
End Sub

'Class1
Public WithEvents frm As MSForms.UserForm

Public Function QClose(cm As Integer, sCap As String) As Boolean

If cm = 0 Then ' little x
QClose = MsgBox(" Sure you want to close " & _
sCap, vbYesNo, "CloseMode = " & 0) = vbNo
Else
MsgBox "Bye"
End If

End Function

Private Sub frm_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

frm.Caption = X & " " & Y ' places text on form
End Sub

Regards,
Peter T



"Michel S." wrote in message
...
Hello !

Much is said in the subject.

Using "Private WithEvents frm AS frmMyForm" gives a compile error :
"object is not a source of automation events"

Using "Private WithEvents frm AS Object" gives a compile error :
"Identifier expected"

Using "Private WithEvents frm AS MSForms.UserForms" works, but it won't
allow me access to some events (such as "QueryClose") I'd like to sink
in the class module.

Any idea ?

Thanks