Announcement

Collapse
No announcement yet.

Dynamically added controls not firing linked class module event handler

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamically added controls not firing linked class module event handler



    At runtime userform is populated with serise of checkboxes

    Code:
    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
    I have a classmodule to handel checkbox event change
    Code:
    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
    With call to class to module
    Code:
    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
    The userform loads properly, however when a checkbox is clicked (thus changing its value) nothing happens; expecting message box.

  • #2
    Re: Dynamically added controls not firing linked class module event handeler.

    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.

    Comment


    • #3
      Re: Dynamically added controls not firing linked class module event handeler.

      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 ?)

      Comment


      • #4
        Re: Dynamically added controls not firing linked class module event handeler.

        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.

        Comment


        • #5
          Re: Dynamically added controls not firing linked class module event handeler.

          Placing handler loop in Activate event worked beautifully.
          Thank you, Thank you, Thank you!

          Comment


          • #6
            Re: Dynamically added controls not firing linked class module event handler

            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

            Comment


            • #7


              Re: Dynamically added controls not firing linked class module event handler

              Thanks for the tip Rory.

              Comment

              Working...
              X