PDA

View Full Version : Solved: Class With Events



MountainVogu
06-15-2006, 02:40 AM
Hi,

please see attached example

I have captured the events of a number of controls in a userform using a class module

I am aware that each control (Label, ComboBox etc) have their on set of event triggers some being common to both. But is it necessary to define a new class for each type of control ?

In the userform Declarations

Dim Cmds() As New Class1
Dim Lbls() As New Class1
Dim Combos() As New Class1

in the class declarations

Public WithEvents CmdControl As MSForms.CommandButton
Public WithEvents LblControl As MSForms.Label
Public WithEvents ComboControl As MSForms.ComboBox



Is it possible to pick up all userform controls and capture the event triggers with more finess than my attempt.

Because not when I try something like

Public WithEvents FrmControls As MSForms.Controls it has a spit

ALSO can someone tell me why the event triggers behind the control itself sometimes differ from that when using the with events in the class. For example no exit event in the class combocontrol but there is on the userform.

Thx a million

Norie
06-15-2006, 06:21 AM
Why do you have MSForms.Controls?

Why not just MSForms.Control?

Killian
06-15-2006, 07:16 AM
I would be inclined to create a class for each type of control - not strictly nessecary, as you've shown, but I can't think of a reason not to.

The only potential shortcut could be to set up a single class with:
Public WithEvents FrmControls As Control
but you'll only have a few common events available (AfterUpdate, BeforeUpdate, Enter, Exit)

Also, I would use a public collection to hold the instances - a collection can hold items of different data types.

So, with a class for each type the initialization would go like thisDim colControls As New Collection
Dim cmd As cCmd
Dim cbo As cCbo
Dim lbl As cLbl

Private Sub UserForm_Initialize()

Dim ctrl As Control
Dim counter As Long

For Each ctrl In UserForm1.Controls
Select Case TypeName(ctrl)
Case "CommandButton"
If ctrl.Name <> "OKButton" Then 'Skip the OKButton
Set cmd = New cCmd
cmd.Init ctrl
colControls.Add cmd
End If
Case "ComboBox"
For counter = 1 To 10
ctrl.AddItem counter
Next
Set cbo = New cCbo
cbo.Init ctrl
colControls.Add cbo
Case "Label"
Set lbl = New cLbl
lbl.Init ctrl
colControls.Add lbl
End Select
Next ctrl
End Sub

MountainVogu
06-15-2006, 02:58 PM
Killian

Thanks mate, watched that horrid match against Paraquay the other night. I think a 1966 repeat is still a few years away yet ! (Pom in Oz)

Yeah I thought that would be the case, haven't needed to play with classes and events too much before, but creating dynamic userforms and objects that require specific event responces necesitates it.

I just wanted to be sure my code wasn't overly verbose (unlike me) or convoluted.

Cheers again.