自定义按钮事件处理类 [英] Class for Custom Button Event Handling

查看:91
本文介绍了自定义按钮事件处理类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)我有一个带有一些按钮的窗体(它在Access中,但我想它也适用于Excel)。

1) I have a Form with some buttons (its in Access, but I guess it applies for Excel as well).

2)我有一个自定义类,可以帮助我调试该表单(以及将来可能添加的表单)。

2) I have a custom class that helps me debug that form (and future forms that I may add).

该类仅记录表单事件触发时的时间,例如加载,卸载,脏,退出等。

The class simply logs when form events fire, such as loaded, unloaded, dirty, exited.

我希望该类具有记录事件的能力。

I'd like that class to have the capability to log when buttons are clicked.

我知道这可以通过使用标准模块并在其中加载公共集合来完成。或者直接使用表单的事件。

I know this can be done by using a standard module, and loading a public collection there. Or by directly using the form's events. Or by storing in a collection behind the form.

但是如果可能的话,我想将其 all 封装在调试类中。然后,在我添加的每个新表单的Form_Load事件中添加了简单的两行。

But I would like, if possible, to encapsulate it all in my debugging class. Then its a simple two lines added to the Form_Load event of each new form I add.

我在下面的简化尝试仅捕获了 last 事件。 em>在类集合中添加的按钮,即Button3。

My simplified attempt below is only capturing the event for the last button that gets added in the class collection, ie. Button3.

TestButtons(具有Button1,Button2和& Button3的表单)

Private Buttons As CButtons

Private Sub Form_Load()
    Set Buttons = New CButtons
    Buttons.LoadButtons Me
End Sub

CButtons(类):

Public WithEvents btn As Access.CommandButton
Private AllButtons As Collection

Const MODE_DEBUG As Boolean = True

Public Sub LoadButtons(ByRef TheForm As Access.Form)

    Dim ctl As Control

    Set AllButtons = New Collection
    For Each ctl In TheForm.Controls
        If ctl.ControlType = acCommandButton Then
            Set btn = ctl
            btn.OnClick = "[Event Procedure]"
            AllButtons.Add btn
        End If
    Next ctl

End Sub

Private Sub btn_Click()
    If MODE_DEBUG Then debug.print btn.Name & "_Click"
End Sub

很想知道是否有人提出建议,谢谢!

Wondering if anyone's got any advice, thanks!

推荐答案

您无法处理集合中的事件。最简单的解决方案是使用一个单独的类来处理按钮事件,在多个按钮处理程序中收集这些类,然后将按钮从处理单个按钮的类传递到处理一个事件中多个按钮的类。

You can't handle events from a collection. The easiest solution is to use a separate class to handle the button events, make a collection of those classes in your multiple buttons handler, and pass the button from the class handling the single button to the class handling multiple ones on an event.

CSingleButton

Public buttonsHandler As CButtons
Public WithEvents btn As Access.CommandButton

Private Sub btn_Click()
    buttonsHandler.HandleClick btn
End Sub

Class CButtons

Private ButtonHandlers As Collection

Const MODE_DEBUG As Boolean = True

Public Sub LoadButtons(ByRef TheForm As Access.Form)

    Dim ctl As Control
    Dim btnHandler As CSingleButton
    Set ButtonHandlers = New Collection
    For Each ctl In TheForm.Controls
        If ctl.ControlType = acCommandButton Then
            Set btnHandler = New CSingleButton
            Set btnHandler.btn = ctl
            Set btnHandler.buttonsHandler = Me
            ctl.OnClick = "[Event Procedure]"
            ButtonHandlers.Add btnHandler
        End If
    Next ctl

End Sub

Public Sub HandleClick(btn As Access.CommandButton)
    If MODE_DEBUG Then debug.print btn.Name & "_Click"
End Sub

这篇关于自定义按钮事件处理类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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