Toggle Button Events Class

Using a class module to control events of multiple controls has been discusses a number of times on this site and elsewhere. But whenever I do it for a new control, I’m going to post it. The control of the day is the Toggle button. Start by creating a class module and calling it CToggles. In the class module, put the following code:

Private WithEvents mToggle As MSForms.ToggleButton
 
Property Set ToggleButtn(oButton As MSForms.ToggleButton)
 
    Set mToggle = oButton
   
End Property
 
Property Get ToggleButtn() As MSForms.ToggleButton
   
    Set ToggleButtn = mToggle
   
End Property
 
Private Sub mToggle_Click()
   
    ‘Put your own meaningful event code here
   If mToggle.Caption = “1” Then
        mToggle.Caption = “2”
    Else
        mToggle.Caption = “1”
    End If
   
End Sub

In a standard module, put this code:

‘Public variable so the toggles don’t go out of scope
Public gToggles() As CToggles
 
Sub AddTogglesToClass()
   
    Dim oleToggle As OLEObject
    Dim i As Long
   
    i = 1
   
    ‘Loop through all the oleobjects and add the toggles to the array
   For Each oleToggle In Sheet1.OLEObjects
        If TypeName(oleToggle.Object) = “ToggleButton” Then
            ReDim Preserve gToggles(1 To i)
            Set gToggles(i) = New CToggles
            Set gToggles(i).ToggleButtn = oleToggle.Object
            i = i + 1
        End If
    Next oleToggle
   
End Sub

Run AddTogglesToClass to get all the ToggleButtons from Sheet1 into the class and thus hooked into the event. You can code whatever you want in the Click event or use any other events available.

For more information, see J-Walk and Associates’ Developer Tip #44 and JKP Application Development Service’s Control Events Article.

Posted in Uncategorized

6 thoughts on “Toggle Button Events Class

  1. Dick,

    Can you explain the purpose of the Let and Get in the Class module? I think it would work the same without them. Also, I learned to do this adding the controls to a collection rather than an array. Do you know if one is preferable over the other?

    Thanks,

    Doug

  2. Juan,

    Thanks. I figured Stephen and company were probably to blame . I’ve switched my behavior with userforms after reading Chapter 10.

    It seems to me this is a little different because the class is a class of togglebuttons already. If the code was changed to use checkboxes instead, then you’d need an analogous class of checkboxes. I don’t understand how the Lets/Gets inside the class make it more insulated. Wouldn’t the encapsulation actually have to be in the standard module so that you could switch from a togglebutton class to a checkbox class?

    I’m over my head here, but just wondering.

    Doug

  3. I’ve been working on this one recently. I can get the grouped controls to work (textboxes in this case) using WithEvents, but I can’t get them to influence other textboxes in the same form. All I’m trying to do is change the Text of the target textbox(es) (called “Value”) based on input from the first textbox (called “Pct”). I wanted to wrap the “Pct” textbox(es) changes in a Class Change event. But the “Value” textbox(es) all come back with null string for their Text properties, even when there is something there. I suspect it has something to do with the instance of the form class versus the events class, but I’m not sure.

    Anybody have any ideas?

  4. Thanks for the fantastic post on event wrappers. I like, particularly, the code to add all the pre-existing toggle buttons to a collection.

    I am having a somewhat related problem. I have been adding controls to a form at runtime (msforms.togglebutton controls, to be exact) and I would like them to be in specific positions on the userform. However, they all show up in the same exact spot (arg) and there don’t seem to be any properties of the togglebutton that can be changed to specify a position on the userform. Do you know how to do this?

    Thanks so much!

    Sydney

  5. Sydney: You can still use Left, Width, Top, and Height even thought they don’t show up in the list.

    Private Sub UserForm_Click()
       
        Dim tb As MSForms.ToggleButton
       
        Set tb = Me.Controls.Add(“Forms.ToggleButton.1”, “tb1”)
       
        tb.Left = 100
       
    End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *