get all the labels of all forms in array
Hi Bart,
I can't see this ever working, or maybe I misunderstand your objective.
Label events will only fire once its parent form is loaded, at which moment
(typically) you would set new instances of withevents class's during the
form's initialize event.
Only for demo (not for use in real life)
Sub LoadAndAddToLabelClass()
Dim vaForms
Dim obFm
Dim i As Long
Dim ctl As msforms.Control
'load the forms
vaForms = Array(UserForm1, UserForm2)
For Each obFm In vaForms
For Each ctl In obFm.Controls
If TypeOf ctl Is msforms.Label Then
Set Labels(i).objLabel = ctl
i = i + 1
End If
Next
Next
End Sub
If you now go on to show the forms the withevents should fire. However, if
any form gets unloaded the withevents will not fire next time you reload,
even though the class's properties & methods will still exist (assuming they
have not been erased or the public array erased).
Regards,
Peter T
"RB Smissaert" wrote in message
...
Need to get all the labels of all forms in an array of custom objects.
This is in connection with control specific help.
Tried like this:
In Class module:
------------------------------
Option Explicit
Public WithEvents objLabel As MSForms.Label
Private Sub objLabel_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)
Dim lContextHelpID As Long
'The following is required because the MouseDown event
'fires twice when right-clicked !!
lClassRightClickCount = lClassRightClickCount + 1
' Do nothing on second firing of MouseDown event
If (lClassRightClickCount Mod 2 = 0) Then
Exit Sub
End If
If Button = 2 Then
On Error Resume Next
lContextHelpID = objLabel.Tag
On Error GoTo 0
If lContextHelpID 0 Then
ShowHelp bWebHelp, lContextHelpID
End If
End If
End Sub
In Normal module:
--------------------------
Public Labels(241) As New LabelClass
Sub AddToLabelClass()
Dim oVBProj As VBProject
Dim oVBComp As VBComponent
Dim i As Long
Dim ctl As MSForms.Control
Set oVBProj = ThisWorkbook.VBProject
For Each oVBComp In oVBProj.VBComponents
If oVBComp.Type = 3 Then
For Each ctl In oVBComp.Designer.Controls
If TypeOf ctl Is MSForms.Label Then
Set Labels(i).objLabel = ctl
i = i + 1
End If
Next
End If
Next
End Sub
I get different error messages and it just doesn't work.
Maybe the trouble is with this:
For Each ctl In oVBComp.Designer.Controls
but haven't found another way yet.
Thanks for any assistance.
RBS
|