+ Reply to Thread
Results 1 to 4 of 4

Userforms - same dblclick event for multiple controls

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-11-2013
    Location
    Austria
    MS-Off Ver
    Excel 2010
    Posts
    136

    Userforms - same dblclick event for multiple controls

    Hello all,

    on a small userform I have about 35 similar label, where a double-click on any of them should update a single textbox.
    I know I could assign something like :
    Private Sub label1_dblclick (....)
    bla bla bla
    end sub
    to each label individually, but can I assign one even to all of them?

    thanks for the help
    michael

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Userforms - same dblclick event for multiple controls

    You use a class to create an object that subscribes to the events raised by the labels, something like:
    Class1:
    Option Explicit
    Private WithEvents p_lbl As MSForms.label
    
    Public Property Set label(lbl As MSForms.label)
        Set p_lbl = lbl
    End Property
    
    Private Sub p_lbl_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
        MsgBox "Hello"
    End Sub
    Userform:
    Option Explicit
    Dim eventHandlers As Collection
    
    Private Sub UserForm_Initialize()
        
        Dim h   As Class1
        Dim ctl As Control
        
        Set eventHandlers = New Collection
        
        For Each ctl In Me.Controls
            If TypeOf ctl Is MSForms.label Then
                Set h = New Class1
                Set h.label = ctl
                eventHandlers.Add h
            End If
        Next ctl
        
    End Sub

  3. #3
    Forum Contributor
    Join Date
    10-11-2013
    Location
    Austria
    MS-Off Ver
    Excel 2010
    Posts
    136

    Re: Userforms - same dblclick event for multiple controls

    Beautiful! Does exactly what I need it to

    Thanks a lot. the Userform code is a bit beyond me at the moment, but Ill sit down and try and figure it out later

    Cheers
    Michael

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Userforms - same dblclick event for multiple controls

    Glad it helps

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] DblClick event on listbox crashes Excel when I move the listbox as a result of the event
    By feanturi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-22-2022, 11:38 AM
  2. [SOLVED] _Change () Event on multiple Controls?
    By onmyway in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-01-2015, 09:07 AM
  3. Lots of controls on userforms
    By Kyle123 in forum The Water Cooler
    Replies: 8
    Last Post: 07-19-2013, 12:23 AM
  4. Editing listbox items in Dblclick event freezes Excel unless mouse moves over listbox
    By muneebmansoor in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-28-2013, 02:21 PM
  5. Controls in Worksheet vs on UserForms
    By LATrojan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-29-2009, 01:18 PM
  6. DblClick event does not work with combobox?
    By marcusr in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-27-2007, 07:44 AM
  7. Event procedures for controls added with Controls.Add
    By John Austin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-09-2005, 01:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1