Results 1 to 10 of 10
  1. #1
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding events dynamically (VB6 SP5)

    Hi, I have found how to add events dynamically to controls in .Net but I can't seem to find how to do it in VB? Is this possible?

    I have a form for data entry with a number of text boxes etc on it. At the bottom there is Save & Close, Undo, Cancel etc which change states depending at which stage the user is (i.e. have they edited something).
    I'm using form_keypress and form_keydown to capture when the press a key (indicating an edit has happened), but the only way I can get the mouse to change the controls is to set a mouse_down event on every single control.

    If it was just the one form it wouldn't be an issue, but I have about 50 to do, all with loads of controls on! I'm after a way to dynamically add a mousedown event for each control on the form. I know how to loop through and check the control types etc, but I don't how to dynamically add an event. Any help would be great thanks.

    Regards,

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding events dynamically (VB6 SP5)

    You could use a single class module which you tie to the controls at runtime. See attached example.

    Unfortunately, not all events are available using tis method.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  4. #3
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding events dynamically (VB6 SP5)

    Oooh I came back from the weekend to a project worth of text formatting! Just finished <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    Thanks Jan Karel, I haven't implemented it into my main project just yet but my initial test worked perfectly!
    I need to read up on classes as i've used them a few times now, but still can't seem to get my head around creating my own.

    Regards,

  5. #4
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding events dynamically (VB6 SP5)

    Just out of interest, is it possible to alter the example so it works for multiple controls i.e. not just textbox, or would I have to make a class for each control type?

    If I pass a different control to it, it returns Type mismatch, which I am assuming is because the code can't set the control to hook as the controls are a different type.

    Private WithEvents mtxtBox As MSForms.TextBox
    Dim bNoEvents As Boolean
    'Allow the calling code to set the control to hook
    Public Property Set Control(txtNew As MSForms.TextBox)
    Set mtxtBox = txtNew
    End Property

  6. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 16 Times in 16 Posts

    Re: Adding events dynamically (VB6 SP5)

    Try replacing MSForms.TextBox with MSForms.Control.

  7. #6
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding events dynamically (VB6 SP5)

    I tried that and get "Object or class does not support the set of events" when Set mtxtBox = txtNew is run.

    I also looked for using Object instead of control but couldn't seem to get that working either.

  8. #7
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Adding events dynamically (VB6 SP5)

    You'll need to set the generic object to a textbox before you can use textbox events and methods.
    Charlotte

  9. #8
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding events dynamically (VB6 SP5)

    You will have to add the object variables and events for each and every type of control you want to handle. For transparency it is probably better to use a separate class module for each one. Doing so will also make it easier to port this to a different project to...
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  10. #9
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding events dynamically (VB6 SP5)

    I thought that might be the case, I attempted to do it as Charlotte suggested but couldnt get it to work. There are only 4 types of object that i'm using so it isn't too much of a problem and my initialise sub now checks the control type and assigns the relevant event.

    However, each of my forms has an EditForm Sub that enables/disables the relevant controls (Close, Save, Cancel etc). They are different for each form so they are subs against the form, not in a module.
    I wrongly assumed that as the event had been created for that form, it would be able to see the forms Subs and Functions.

    I solved this by using the Parent command:
    Dim tmp As Object
    Set tmp = mtxtBox.Parent
    Call tmp.editform

    This now works perfectly. All I need to do is put Call InitialiseEditForm(frmName) in the forms load event and each input control on the form has a mousedown, keypress and keydown event added to it.

    Thanks loads for all of your help. <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23>

    Regards,

  11. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding events dynamically (VB6 SP5)

    Yup, that is the way to do this. Great you got it sorted.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

Posting Permissions

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