如何为多个按钮分配通用步骤? [英] How to assign a common procedure for multiple buttons?

查看:50
本文介绍了如何为多个按钮分配通用步骤?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前段时间,我有一个示例代码为多个按钮分配相同的过程.
像这样:

Some time ago I have a sample code to assign the same procedure for multiple buttons.
Something like this:

For Eeach buttton in Form1
If button is clicked
MsgBox button.Caption.

但是我现在找不到此代码.
谷歌搜索,我找不到我所需要的东西.
我只记得我需要插入一个类模块.
有人可以给我一个链接或一个简短的例子.

But I can't find this code now.
Googling I cant found egzactly what I need.
I just remember that I need to insert a class module.
Could someone give me a link or a short example.

推荐答案

插入一个新的类模块,并将其命名为 clListener (我才想到).
其中的代码:

Insert a new Class Module and name it clListener (that just came to my mind).
Code in there:

Public WithEvents ct As MSForms.CommandButton

Public Sub ct_Click()
    MsgBox ct.Name & " clicked!"
End Sub

在用户窗体模块中:

Private listenerCollection As New Collection

Private Sub UserForm_Initialize()
    Dim ctItem
    Dim listener As clListener

    For Each ctItem In Me.Controls
        If TypeName(ctItem) = "CommandButton" Then
            Set listener = New clListener
            Set listener.ct = ctItem
            listenerCollection.Add listener
        End If
    Next
End Sub

这篇关于如何为多个按钮分配通用步骤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆