用户表单中所有切换按钮的一种颜色更改代码多页
我有大约 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面的示例创建了一个新类,以便使用一个事件处理程序处理多个切换按钮。请注意,它假设多页控件的第一页包含切换按钮。相应地更改页面引用。
首先插入一个新的类模块(插入>>类模块),并将其命名为clsToggleButton。
然后将以下代码复制并粘贴到新类的代码模块中。 。 。
然后将以下代码复制并粘贴到您的用户窗体代码模块中。 。 。
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 . . .
Then copy and paste the following code into your userform code module . . .
我已经很多年没有使用 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.