Home > Back-end >  VBA One color change code for all ToggleButtons in a Userforms' Multipage
VBA One color change code for all ToggleButtons in a Userforms' Multipage

Time:03-19

Hello everyone!

As the title says, I have some ToggleButtons here (around 100). What i would like to do with them, that

If .value = true then
 togglebuttons.BackColor = vbRed
Else 
  = vbGreen

I can do this by writing the code for every single one of them, but is there a way to create a group or class so that color change code would be applied to all of them? I just want it to look clean and not to have 100 codes for every button in there if possible. Thank you for any juicy solutions. -Excel365

CodePudding user response:

Here's an example that creates a new class in order to handle multiple toggle buttons using one event handler. Note that it assumes that the first page of your multipage control contains your toggle buttons. Change the page reference accordingly.

First insert a new class module (Insert >> Class Module), and name it clsToggleButton.

Then copy and paste the following code into the code module for your new class . . .

Option Explicit

Public WithEvents toggleButton As MSForms.toggleButton

Private Sub toggleButton_Click()
    
    With toggleButton
        If .Value = True Then
            .BackColor = vbRed
        Else
            .BackColor = vbGreen
        End If
    End With
    
End Sub

Then copy and paste the following code into your userform code module . . .

Option Explicit

Dim toggleButtonCollection As Collection

Private Sub UserForm_Initialize()
    
    Set toggleButtonCollection = New Collection
    
    Dim ctrl As MSForms.Control
    Dim cToggleButton As clsToggleButton
    
    For Each ctrl In Me.MultiPage1.Pages(0).Controls
        If TypeName(ctrl) = "ToggleButton" Then
            'ctrl.BackColor = vbGreen 'uncomment to initially set the backcolor to green
            Set cToggleButton = New clsToggleButton
            Set cToggleButton.toggleButton = ctrl
            toggleButtonCollection.Add cToggleButton
        End If
    Next ctrl
    
End Sub

CodePudding user response:

I have not worked with VB for many years and it was .net, so, if this solution is incorrect, let me know.

Solution 1: Arrays or Lists

You can create an array or a list containing all your toggle buttons, loop them and perform the operation you need for each of them. This will make sure that the logic above would be implemented exactly once rather than duplicated, yet, you still need to build your collections with the buttons.

Solution 2: A class

You can create a subclass for your toggle buttons and make sure that every toggle button in question will be of that class. And then you can create a static List for the class. In the constructor of each toggle button you append that button to the shared list in the class. And then you can create a shared method that loops the list and performs the logic you need.

P.S. Sorry for not writing code, I no longer remember the syntax of the language.

  • Related