Passing a Userform to a Class

H

Howard Kaikow

I'd like to be able to pass a Userform to a Class so the Class can perform
the Userform events.
The difficulty appears to be that when I pass the Userform object to the
class, the Userform Initialize event fires before the class can do anything.

Is there a way around this?

At present, I've tried the following:

In a normal code module, where frmY is the Userform:

Public clsX As X
Public Sub RunTests()
Set clsX = New X
clsX.SetClass frmY
frmY.Show
Set clsX = Nothing
End Sub

And in the Class:

Private WithEvents frmLocal As MSForms.UserForm

Public Sub SetClass(frmIs As MSForms.UserForm)
Set frmLocal = frmIs
End Sub

Private Sub frmLocal_Initialize()
With frmLocal.lst
' Do initialize stuff here
End With
ClearList
End Sub

Private Sub Class_Terminate()
Set frmLocal = Nothing
End Sub

frmLocal_Initialize does not fire, I guess because Userform_Initialize is
firing.

Can I stop the Userform_Initialize event from firing?
Do I need to do so?
 
H

Howard Kaikow

The problem seems to be that when I pass the Userform to SetClass, the
Userform_Initialize event gets invoked, so I have to change my code
accordingly.
 
O

Oneide

Yes, it is.
As soon as the UserForm got used (set) it fires the initialize event.
So, you cannot prevent it to do such a thing. It's by design. In fact, even
in VB.
Another aproach, if I understood your point, would be to declare a public
sub in your userforms that could be invoked by your class (for initializing
purposes). Something like this:

:: In your UserForm module
Public Sub Init()
'Do whatever you want
End Sub

:: In your Class module
Public Sub SetClass(frmIs As MSForms.UserForm)
Set frmLocal = frmIs
'Move your class initialize code here
With frmLocal.lst
' Do initialize stuff here
End With
ClearList
'Now, force the code behind your userform to execute
frmLocal.Init
End Sub

:: end of code

Am I pointing to the right direction?


Howard Kaikow <[email protected]> escreveu nas notícias de
mensagem:[email protected]...
| The problem seems to be that when I pass the Userform to SetClass, the
| Userform_Initialize event gets invoked, so I have to change my code
| accordingly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top