Possibly because the array in the class module only knows about (any) checkboxes that exist when the userform loads.
If you load a new checkbox, you have to add it to the class.
At runtime userform is populated with serise of checkboxes
I have a classmodule to handel checkbox event changeVB:For Each c In Range("Weapons") Set Weap = MultiPage1.Pages(0).Controls.Add("Forms.CheckBox.1", Visible) Weap.ControlTipText = c.Offset(0, 1) Weap.Top = CtrlTop Weap.Height = 18 Weap.Left = 6 Weap.Width = 108 Weap.WordWrap = False Weap.Locked = False Weap.TextAlign = fmTextAlignLeft Weap.Caption = c.Offset(0, 0) CtrlTop = CtrlTop + CtrlHeight + CtrlGap Next c
With call to class to moduleVB:Option Explicit Public WithEvents CheckBoxEvents As MSForms.CheckBox Property Get CheckBox() As MSForms.CheckBox Set CheckBox = CheckBoxEvents End Property Property Set CheckBox(ChkHd As MSForms.CheckBox) Set CheckBoxEvents = ChkHd End Property Private Sub CheckBoxEvents_Change() With CheckBoxEvents If .Value Then MsgBox "Item picked" End If End With End Sub
The userform loads properly, however when a checkbox is clicked (thus changing its value) nothing happens; expecting message box.VB:Dim AobjCheckBoxes() As New clsLegCheck Private Sub UserForm_Initialize() Dim intCtlCnt As Integer, objControl As Control For Each objControl In Me.Controls If TypeOf objControl Is MSForms.CheckBox Then intCtlCnt = intCtlCnt + 1 Redim Preserve AobjCheckBoxes(1 To intCtlCnt) Set AobjCheckBoxes(intCtlCnt).CheckBoxEvents = objControl End If Next objControl Set objControl = Nothing
Possibly because the array in the class module only knows about (any) checkboxes that exist when the userform loads.
If you load a new checkbox, you have to add it to the class.
The .Add is executed after the Initialize event has run.
Restated, the userform has to be Initialized before a control can be added to it.
If the loop adding objects to the array is moved to the userform's Activate event, it should work as expected.
Alternatly,
Have you considered a multi-select list box instead of creating check-boxes on the fly.
(? .ListStyle = fmListSyleOption ?)
I did consider a list box, however there is additional data for each item that the user will want to review before making a selection so I ruled out the list box option as not being able to handle all requirements for the page. The page needs to have check boxes loaded on initialize because customer will be able to add new items or modify existing items in spreadsheet upon ownership of base product. Without code to create userform at initialize the product would break down if spreadsheet is modified.
I'll give the userform Activate event suggestion a try.
Thanks.
Placing handler loop in Activate event worked beautifully.
Thank you, Thank you, Thank you!
Just FYI, I'd suggest you use Typename(objControl) = "CheckBox", rather than TypeOf, since togglebuttons, optionbuttons and checkboxes all implement the CheckBox interface and would all be picked up by your TypeOf check.
Rory
Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why
Thanks for the tip Rory.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks