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.

4 Comments

  1. Doug Glancy says:

    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 Pablo González says:

    Doug,

    Maybe this post will explain it a bit ? Dick took quite a hit the last time for not using “proper” coding techniques :D

    http://www.dicks-blog.com/archives/2005/05/23/getting-data-from-a-userform/

  3. Doug Glancy says:

    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

  4. Scott says:

    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?

Leave a Reply