+ Reply to Thread
Results 1 to 14 of 14

Programatically adding event handlers to userform objects

  1. #1
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Programatically adding event handlers to userform objects

    I have a userform where the objects are added programmatically. Can I also add mouseover/click events to the objects? The number of objects is determined at run time so unable to add code at design time.
    Please click *Add Reputation if I've helped

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Programatically adding event handlers to userform objects

    Perhaps this will help.

    Extensibility: Programming The VBA Editor
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Programatically adding event handlers to userform objects

    Hi,

    If the code is the same for groups/types of control you can use classes to handle the events. If it isn't, you would have to add the code through the Designer while the form is closed.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  4. #4
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Programatically adding event handlers to userform objects

    Thanks so much for your responses.

    Quote Originally Posted by AlphaFrog View Post
    The userform is designed to be run on colleagues machines and while I can reference a library, I'd rather not have to get them to trust access to the VBProject.

    Quote Originally Posted by xlnitwit View Post
    Hi,

    If the code is the same for groups/types of control you can use classes to handle the events. If it isn't, you would have to add the code through the Designer while the form is closed.
    Effectively, when the mouse is over a help icon for a textbox (just a label with the caption "(?)"), I want the caption of another label on the form to populate with help text for that textbox. The help text itself I can store in the tag property for the help icon.

    So, each help icon will have a different name obvs, but the code just needs to get the tag for that help icon and display it, so object name aside the code will be the same. My naming conventions mean that the name for each of these help icons start with the letters "hlp" and will be the only objects on the userform that do so. As I write this, your plan sounds very possible. Could you help me further?

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Programatically adding event handlers to userform objects

    Can you provide a sample workbook to save me creating one from scratch? If not, I will attempt to provide some sample code tomorrow.

  6. #6
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Programatically adding event handlers to userform objects

    Thanks man. Sample attached. This is part of a much larger project but everything you need should be here. The purpose of the userform is to allow a user to edit "settings" that would be used in the rest of the code.
    Attached Files Attached Files
    Last edited by kadeo; 09-20-2016 at 04:25 AM.

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Programatically adding event handlers to userform objects

    Thank you. Which label(s) should contain the help text?

  8. #8
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Programatically adding event handlers to userform objects

    In the DisplaySettings procedure of the UserForm, the third block that creates objects where the caption is set as " (?) " and the name of the label is appended to " hlp ". This bit:
    Please Login or Register  to view this content.
    On the UserForm itself, it's the column of " (?) " to the right hand side.
    Last edited by kadeo; 09-20-2016 at 05:24 AM.

  9. #9
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Programatically adding event handlers to userform objects

    Oh wait, I just realised you probably meant which label will display the help text. Haven't got around to creating that yet, but I was gonna place it in the space between the "buttons" at the top and the frame.

  10. #10
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Programatically adding event handlers to userform objects

    That doesn't allow very much room to display the text. Are you expecting the form to resize?

  11. #11
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Programatically adding event handlers to userform objects

    I'm just adding the functionality at the moment, I'm gonna worry about aesthetics later.

  12. #12
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Programatically adding event handlers to userform objects

    I had not in fact seen your last post- I was referring to the help labels themselves.

    Herewith a simplistic example of a class-based approach. Clicking each label will update the help text in the newly added textbox.
    Attached Files Attached Files
    Last edited by xlnitwit; 09-20-2016 at 05:50 AM.

  13. #13
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Programatically adding event handlers to userform objects

    Like you said, it's so simple. You didn't add much code at all. This is doing exactly what I need now, thanks so much for your help.

    Man, I wish I could get to grips with classes, they've eluded me for so long.

  14. #14
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Programatically adding event handlers to userform objects

    You're welcome.

+ 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. Dynamically assigned event handlers disappear
    By terra.rubens in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-11-2015, 11:56 AM
  2. AddingControls and event handlers RUNTIME
    By Ahkio in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-17-2015, 08:32 PM
  3. [SOLVED] Dynamic Controls with event handlers
    By stuart010 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-02-2012, 11:43 AM
  4. (Closed)Dynamic Userform Controls With Event Handlers
    By stuart010 in forum Excel General
    Replies: 0
    Last Post: 04-30-2012, 11:32 AM
  5. Adding objects within userform with condition...
    By contaminated in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-30-2010, 12:31 PM
  6. Run-Time Error 91 When Dynamically Creating Controls and Event Handlers
    By chucklod in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-21-2008, 05:47 PM
  7. Further Clarification with Event Handlers & Class Modules
    By Kevin H. Stecyk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-25-2005, 02: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