re-assign a control to new class event handler

RAYLWARD102

Well-known Member
Joined
May 27, 2010
Messages
529
Is it possible?
During control creation, you can assign a class event routing (click, change events.....) to a UserForm control (My method below)
Code:
                Set C1 = New Class1
                Set C1.labelz = LAB


Is there any possibility for re-assigning a control that has already been assigned

Code:
         'Set to label routing 
                Set C1 = New Class1
                Set C1.labelz = LAB

         'After some control has been clicked, re-purpose the control by assigning a new class routing
         Set C1.labelz3 = LAB
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
forgot to mention the reason why I want to do this; I wanted to create some generic routines that have many dynamically created controls. I wanted to repurpose these controls, exchange event routines from time to time without clearing and re-creating all the controls; and do not wish to duplicate large sums of code, only for the purpose of changing an event class routing for 1 control. I realize I could use some variable in the class event routine, to distinguish what happens in the event, but wanted to know if there's a cleaner way, like re-assigning a different class event to an existing control
 
Last edited:
Upvote 0
How are you storing the controls you are assigning to the class(es)?
 
Upvote 0
A collection; MEM1

Code:
        Set C1 = New PlatformControls
        Set C1.labelzA = LAB
        MEM1.Add Item:=C1, Key:=.Name

This is what I've tried to make the labelzA do; obviously doesn't work, but gives you the direction I'm trying to seek
below are the event class routines
Code:
Private Sub labelzA_Click()
    Dim q As String
    q = InputBox("enter 2 to transform this control class routine", "experiment", "1")
    
    Select Case q
        Case "2"
            With labelzA
                Set C1.labelzB = LAB
            End With
    End Select
    
End Sub
Private Sub labelzB_Click()
    MsgBox "sucess"
End Sub


Here is the first control in userform
Code:
Private Sub UserForm_Initialize()
    Set MEM1 = New Collection
    
    Set LAB = UserForm1.Controls.Add("Forms.Label.1")
    With LAB
        .Caption = ".click:"
        .Height = 100
        .Width = 500
        .Left = 0
        .Top = 0
        .SpecialEffect = fmSpecialEffectFlat
        .Name = "SearchDisplay"
        .Font.size = .Height * 0.5
        .BackStyle = fmBackStyleOpaque
        .BackColor = vbWhite
        .ForeColor = RGB(192, 192, 192)
        .TextAlign = fmTextAlignCenter
        Set C1 = New PlatformControls
        Set C1.labelzA = LAB
        MEM1.Add Item:=C1, Key:=.Name
    End With
            
End Sub
 
Last edited:
Upvote 0
I think what you might need is multiple collections with each collection associated with a particular class.

When you want to re-assign a control to another class you remove it from the relevant collection and add it to its 'new' collection.

Of course, that's all theory but sounds, to me anyway, kind of right.:eek:
 
Upvote 0
Haven't had much luck iterating (previous attempts) through collections; do you have an example of how I'd find the named control in the collection and remove it?
one would think; but below doesn't work
Code:
    For x = 0 To MEM1.Count
        Debug.Print MEM1.Item(x)
        
        
    
    Next x
 
Last edited:
Upvote 0
I think you might need to look at using the key to remove the control from the collection.
Code:
MEM1.Remove "SearchDisplay"
 
Upvote 0
Below sort of works; except the control now fires both labelza and labelzb routines after first click.

Code:
Private Sub labelzA_Click()
    MsgBox "test"
    MEM1.Remove labelzA.Name
    With labelzA
        Set C1.labelzB = LAB
        MEM1.Add Item:=C1, Key:=labelzA.Name
    End With
End Sub
Private Sub labelzB_Click()
    MsgBox "sucess"
End Sub
 
Last edited:
Upvote 0
Working Now!!! yeah. Thanks Norie!
Added Set C1 = New PlatformControls
Code:
Private Sub labelzA_Click()
    MsgBox "test"
    MEM1.Remove labelzA.Name
    With labelzA
        Set C1 = New PlatformControls
        Set C1.labelzB = LAB
        MEM1.Add Item:=C1, Key:=labelzA.Name
    End With
End Sub
Private Sub labelzB_Click()
    MsgBox "sucess"
End Sub
 
Upvote 0
My Last question is; how can iterate through all controls and identify those belonging to labelza?
Would like to loop thorugh; I know how to loop the controls but not how to identify the assigned event class name labelza

Code:
dim CNTRL as object

    For Each CNTRL In UserForm1.Controls
        If "control event name is labelza" Then
        
        End If
    Next CNTRL
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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