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:

In a standard module, put this code

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.

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?



  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.


  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!


  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

Posting code? Use <pre> tags for VBA and <code> tags for inline.

Leave a Reply

Your email address will not be published.