在自定义类中向/从用户名读取变量/从自定义类返回变量值-VBA Excel

问题描述 投票:0回答:2

我有一个基于CommandButton类的自定义创建的类btnClass。

Public WithEvents ButtonEvent As MsForms.CommandButton

Private Sub ButtonEvent_Click()

End sub

我有一个UserForm1,它具有一个ListBox,一个Label和数百个动态创建的CommandButton。我将btnClass分配给Buttons。当单击按钮时,我希望Click事件具有以下结果:

  • 如果选择的按钮数量(selQty)小于Label.Caption(总数),并且以前没有选择此按钮,请向listBox添加值并更改BackColor。
  • 如果先前选择了此按钮,请更改颜色并将所选按钮的数量(selQty)减少1。

我尝试创建Public变量,但无法获得所需的结果。这可行吗?

P.S。当激活UserForm1时,表示未选择任何按钮;当我单击按钮时,我更改按钮的颜色并接受选择。

excel vba class variables methods
2个回答
0
投票

这里有些东西可以帮助您入门。

创建一个包装每个按钮并处理其click事件的包装器类。然后,在加载表单时,循环浏览控件并包装按钮。

需要一个模块级别的集合来保存已包装按钮(包装类)的引用。

ButtonWrapper类:

Option Explicit

Private WithEvents objButton As MsForms.CommandButton

'Wrap button
Public Function WrapCommandButton(btn As MsForms.CommandButton) As ButtonWrapper
    Set objButton = btn
    Set WrapCommandButton = Me
End Function

'Button's event handler
Private Sub objButton_Click()
    MsgBox objButton.Caption & " was clicked."
End Sub

'Clean up
Private Sub Class_Terminate()
    Set objButton = Nothing
End Sub

表格后面的代码:

Option Explicit

Private m_handlers As Collection

'Initialize
Private Sub UserForm_Initialize()

    Set m_handlers = New Collection

    Dim ctl As Control

    For Each ctl In Me.Controls
        If TypeName(ctl) = "CommandButton" Then
            With New ButtonWrapper
                m_handlers.Add .WrapCommandButton(ctl)
            End With
        End If
    Next ctl
End Sub

'Clean up
Private Sub UserForm_Terminate()
    Set m_handlers = Nothing
End Sub

Form

enter image description here

希望这会有所帮助。


0
投票

似乎您想做这样的事情

clsButtonClick:

Option Explicit

Public WithEvents ButtonEvent As MSForms.CommandButton

Private Sub ButtonEvent_Click()
    'pass the button to the procedure in the userform
    ButtonEvent.Parent.HandleClick ButtonEvent
End Sub

用户代码:

Option Explicit

Const CLR_SEL As Long = vbRed         'selected color
Const CLR_NOT_SEL As Long = vbGreen   'unselected color

Dim btnCol As Collection
Dim maxQty As Long   'max number selectable
Dim currQty As Long  'number currently selected

'perform some setup
Private Sub UserForm_Activate()
    Const NUM_BUTTONS As Long = 10
    Dim i As Long, btn As MSForms.CommandButton
    Dim o As clsButtonClick

    currQty = 0  'number selected
    maxQty = 5   'max selectable
    Set btnCol = New Collection
    'add some buttons
    For i = 1 To NUM_BUTTONS
        Set btn = Me.Controls.Add("Forms.CommandButton.1", "btn" & i, True)
        btn.BackColor = CLR_NOT_SEL
        btn.Height = 18
        btn.Left = 20
        btn.Top = 20 * i
        btn.Caption = "Button " & i
        Set o = New clsButtonClick
        Set o.ButtonEvent = btn
        btnCol.Add o
    Next i
End Sub

'handle a button click event (button is passed in)
Sub HandleClick(btn As MSForms.CommandButton)
    If btn.BackColor = CLR_SEL Then
        btn.BackColor = CLR_NOT_SEL
        currQty = currQty - 1
    Else
        If currQty = maxQty Then
            MsgBox "no more selections available"
        Else
            btn.BackColor = CLR_SEL
            currQty = currQty + 1
        End If
    End If
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.