简体   繁体   English

用户表单多页中所有 ToggleButton 的一个颜色更改代码

[英]One color change code for all ToggleButtons in a Userforms' Multipage

I have around 100 ToggleButtons.我有大约 100 个切换按钮。

I would like:我想要:

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

I can write the code for every one, but is there a way to create a group or class so that color change code would be applied to all of them?我可以为每个人编写代码,但是有没有办法创建一个组或 class,以便将颜色更改代码应用于所有这些代码?

-Excel365 -Excel365

Here's an example that creates a new class in order to handle multiple toggle buttons using one event handler.下面是创建一个新的 class 的示例,以便使用一个事件处理程序处理多个切换按钮。 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.首先插入一个新的class模块(Insert >> Class Module),命名为clsToggleButton。

Then copy and paste the following code into the code module for your new class. .然后将以下代码复制并粘贴到新 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

I have not worked with VB for many years and it was .net, so, if this solution is incorrect, let me know.我已经很多年没有使用 VB 了,它是 .net,所以,如果这个解决方案不正确,请告诉我。

Solution 1: Arrays or Lists解决方案 1:Arrays 或列表

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.这将确保上面的逻辑只执行一次而不是重复执行,但是,您仍然需要使用按钮构建 collections。

Solution 2: A class方案二: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.您可以为您的切换按钮创建一个子类,并确保每个有问题的切换按钮都是 class。然后您可以为 class 创建一个 static 列表。在每个切换按钮的构造函数中,您将 append 那个按钮共享列表在 class 中。然后您可以创建一个共享方法来循环列表并执行您需要的逻辑。

PS Sorry for not writing code, I no longer remember the syntax of the language. PS 很抱歉没有编写代码,我不再记得该语言的语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM