Consulting

Results 1 to 2 of 2

Thread: Dynamically Added Checkbox Is Not Shown While Clicked

  1. #1

    Dynamically Added Checkbox Is Not Shown While Clicked

    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

  2. #2
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    836
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •