Subclasses of UserForm Controls?

djr8yk

Board Regular
Joined
May 19, 2015
Messages
58
Is it possible to create a subclass of a UserForm Control, like a CheckBox?
I want to have a CommandButton that is somehow linked with an array of CheckBoxes. I'm dynamically creating some CommandButtons and CheckBoxes, and some of the boxes must be checked before the button can be pressed. Any idea how to create this subclass?
Thanks
 
Hi All.

If you use the clsBpca class module which I released, you can program it as follows.


You become able to describe the event handling of Control-Array in UserForm module in the same way as VB6.
It is necessary to take 3 modules (clsBpca, clsBpcaCh, modBpcaConst) in workbook by import from
a distribution file to use this class.
The editing of the clsBpca class module is unnecessary (you should do even import).

AddinBox( Breakthrough in the Pseudo Control Array )
AddinBox( Breakthrough in the Pseudo Control Array : clsBpca Reference )


Code:
  [x]CheckBox1    [CommandButton1]
  [x]CheckBox2    [CommandButton2]
  [x]CheckBox3    [CommandButton3]
  [x]CheckBox4    [CommandButton4]

 '=== UserForm module ===

' Buttons is a Control-Array variable for CommandButton1-4.
Private WithEvents Buttons As clsBpca


' CheckBtns is a Control-Array variable for CheckBox1-4.
Private WithEvents CheckBtns As clsBpca


Private Sub UserForm_Initialize()
Dim j As Integer


    Set Buttons = New clsBpca
    Set CheckBtns = New clsBpca
    
    'Initialize as CommandButton(Enabled=False) & CheckBox(Value=False)
    'Link to CommandButton by Tag property of CheckBox.
    For j = 1 To 4
        Me.Controls("CommandButton" & j).Enabled = False
        Me.Controls("CheckBox" & j).Value = False
        Me.Controls("CheckBox" & j).Tag = "CommandButton" & j
        
        'Add control to Control-Array.
        Buttons.Add Me.Controls("CommandButton" & j)
        CheckBtns.Add Me.Controls("CheckBox" & j)
    Next j
    
    'Generate Control-Array (with Click event)
    Buttons.Rgst BPCA_Click
    CheckBtns.Rgst BPCA_Click
End Sub


Private Sub UserForm_Terminate()
    Buttons.Clear
    CheckBtns.Clear
    Set Buttons = Nothing
    Set CheckBtns = Nothing
End Sub


'=== You can program Control-Array in the same way as VB6 in clsBpca. ===


'Any processing when you clicked CommandButton1-4.
Private Sub Buttons_Click(ByVal Index As Integer)
    MsgBox Buttons(Index).Name & " is Clicked"
End Sub


'Any processing when you clicked CheckBox1-4.
Private Sub CheckBtns_Click(ByVal Index As Integer)
   'Change Enable/Disable of CommandButton by CheckBox.
    With CheckBtns(Index)
        Me.Controls(.Tag).Enabled = .Value
    End With
End Sub



If you control plural CommandButton in one CheckBox,
you should set the same group name in both tag properties of CheckBox and CommandButton.
Code:
'Any processing when you clicked CheckBox1-4.
Private Sub CheckBtns_Click(ByVal Index As Integer)
Dim ctrl As MsForms.Control


   'Change Enable/Disable of CommandButton by CheckBox.
    For Each ctrl In Me.Controls
      If Not (ctrl Is CheckBtns(Index)) Then
          If (CheckBtns(Index).Tag = ctrl.Tag) Then
              ctrl.Enabled = CheckBtns(Index).Value
          End If
      End If
    Next
End Sub





( Because I who am poor at English am translating into English
while using translation software, there may be an odd expression. )
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,638
Latest member
Oluwabukunmi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top