用户表单中所有切换按钮的一种颜色更改代码多页

发布于 2025-01-15 02:07:58 字数 246 浏览 6 评论 0原文

我有大约 100 个切换按钮。

我想:

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

我可以为每个代码编写代码,但是有没有办法创建一个组或类,以便将颜色更改代码应用于所有代码?

-Excel365

I have around 100 ToggleButtons.

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?

-Excel365

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给我一枪 2025-01-22 02:07:58

下面的示例创建了一个新类,以便使用一个事件处理程序处理多个切换按钮。请注意,它假设多页控件的第一页包含切换按钮。相应地更改页面引用。

首先插入一个新的类模块(插入>>类模块),并将其命名为clsToggleButton。

然后将以下代码复制并粘贴到新类的代码模块中。 。 。

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

然后将以下代码复制并粘贴到您的用户窗体代码模块中。 。 。

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

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
时光沙漏 2025-01-22 02:07:58

我已经很多年没有使用 VB 了,它是 .net,所以,如果这个解决方案不正确,请告诉我。

解决方案 1:数组或列表

您可以创建 数组 或包含所有内容的列表您的切换按钮, 循环它们并为它们中的每一个执行您需要的操作。这将确保上述逻辑仅执行一次而不是重复,但是您仍然需要使用按钮构建集合。

解决方案 2:类

您可以创建一个 子类 为您的切换按钮,并确保每个有问题的切换按钮都属于该类。然后您可以为该类创建一个静态列表。在每个切换按钮的构造函数中,将该按钮附加到类中的共享列表中。然后您可以创建一个共享方法来循环列表并执行您需要的逻辑。

PS 抱歉没有写代码,我不再记得该语言的语法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文