Thanks for your reply, Bob..
if you don't want a control array, what is the purpose
of the event sink class?
Because you can instantiate it more than once anywhere in your
application..
Let's take the same example as yours, but without control arrays; I'll
even add a third line and sink click events for the two buttons in the
class module :
=== ' form module
Option Explicit
Private objLine1 As clsLine
Private objLine2 As clsLine
Private objLine3 As clsLine
Private Sub UserForm_Initialize()
Set objLine1 = New clsLine
Set objLine2 = New clsLine
Set objLine3 = New clsLine
objLine1.SetCtls txtLine1_1, txtLine2_1, cmdLine1_1, cmdLine1_2
objLine2.SetCtls txtLine2_1, txtLine2_1, cmdLine1_1, cmdLine1_2
objLine2.SetCtls txtLine3_1, txtLine2_1, cmdLine1_1, cmdLine1_2
End Sub
==== ' clsLine
Option Explicit
Private WithEvents mtxt1 As MSForms.TextBox
Private WithEvents mtxt2 As MSForms.TextBox
Private WithEvents mcmd1 As MSForms.CommandButton
Private WithEvents mcmd2 As MSForms.CommandButton
' this could also be replaced by 4 Property Set procs
' but since we always assign the four controls, why not use
' a single assignment routine ?
Public Sub SetCtls(txt1 As MSForms.TextBox, txt2 As MSForms.TextBox, _
cmd1 As MSForms.CommandButton, cmd2 As MSForms.CommandButton)
Set mtxt1 = txt1 'you don't have to match the form's names.
Set mtxt2 = txt2
Set mcmd1 = cmd1
Set mcmd2 = cmd2
End Sub
Private Sub mtxt1_Change()
SetButtons
End Sub
Private Sub mtxt2_Change()
SetButtons
End Sub
Private Sub mcmd1_Click()
'...
End Sub
Private Sub mcmd2_Click()
'...
End Sub
Private Sub SetButtons()
mcmd1 = mtxt1.Value <> 0 And mtxt1.Value <> vbNullString
mcmd2 = mtxt2.Value > mtxt1.Value
End Sub
I think this method pros are :
- you instantiate the class where you want it, as many times as you
need
- you dont have to "twist" you controls names to fit a given pattern
- controls names are resolved at compile time instead of at run time
- easier to read/debug
Cons:
- need more code lines in the calling module when the number of
instances grows..
- ... (can't find others - do you ?)
In an Access application, I once designed a "calendar" class,
containing a textbox, a button and a calendar control popup form. The
user can enter a date in the textbox, use arrow keys to change days,
click on the button to pop the calendar control (which takes the
textbox value on enter and sets it on exit) as he wish.
This class can be used on any form.. and you don't need to have more
than one instance on the same form (which is a requirement of a control
array).
Nevertheless, I learned something new with your method, and I can think
of some situations where I can use it.
Thanks again for your input !
Michel
Bob Phillips a présenté l'énoncé suivant :