PDA

View Full Version : Dynamically Added Checkbox Is Not Shown While Clicked



jonny
01-08-2019, 05:53 AM
Hi there,
The below code populates 10 CheckBoxes,
While clicking one of them I wish to show the number of clicked checkbox.
Will much appreciate your help with figuring out, why the below doesn't work..

Thanks
Module:


Option Explicit


Dim chkArray() As New clsCheck


Private Sub Test()
Dim NewCheckBox As MSForms.CheckBox
Dim NewLable As MSForms.Label
Dim NewTextBox As MSForms.TextBox
Dim PartType, TempForm
Dim X As Integer

Application.VBE.MainWindow.Visible = False
Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)


'Create the User Form
With TempForm
.Properties("Caption") = "Test"
.Properties("Width") = 450
.Properties("Height") = 300
End With


For X = 1 To 10
Set NewCheckBox = TempForm.designer.Controls.Add("Forms.checkbox.1")
With NewCheckBox
.Name = "NewCheckBox" & X + 1
.Caption = X
.Value = False
.Top = 20 + (12 * X)
.Left = 250
.Width = 100
End With
ReDim Preserve chkArray(1 To X)
Set chkArray(X).myCheckBox = NewCheckBox
Next X


'Show the form
VBA.UserForms.Add(TempForm.Name).Show
End Sub


Class Module:


Public WithEvents myCheckBox As MSForms.CheckBox


Private Sub myCheckBox_Click()
MsgBox myCheckBox.Caption
End Sub

Dave
01-08-2019, 09:54 AM
ReDim Preserve chkArray(0 To X-1)
Set chkArray(X-1).myCheckBox = NewCheckBox
HTH. Dave
edit: U could just put Option Base 1 at the top of your code sheet