Results 1 to 13 of 13

Thread: [RESOLVED] Dynamic runtime control event handling

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Posts
    95

    [RESOLVED] Dynamic runtime control event handling

    How can I handle an event for a control that does not yet exist?
    Last edited by Kt3; May 13th, 2004 at 09:56 AM.

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    You would create a generic sub
    VB Code:
    1. Private Sub MyEventHandler(ByVal sender As Object, ByVal e As EventArgs)
    2. End Sub

    Then use the AddHandler method to attach the control's event to the sub
    VB Code:
    1. Dim b As New Button
    2.  
    3. AddHandler b.Click, New EventHandler(AddressOf MyEventHandler)
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Posts
    95
    Thanks a lot for the info! That's exactly what I'm looking for.

  4. #4
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    Just remember to use
    VB Code:
    1. RemoveHandler b.Click, New EventHandler(AddressOf MyEventHandler)

    when disposing or removing the object.

    If you continously create and remove objects, and adding handlers to all of them, you will have a lot of "dead" references taking up memory.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  5. #5
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by pax
    Hi.

    Just remember to use
    VB Code:
    1. RemoveHandler b.Click, New EventHandler(AddressOf MyEventHandler)

    when disposing or removing the object.

    If you continously create and remove objects, and adding handlers to all of them, you will have a lot of "dead" references taking up memory.
    hmmm I think when I was learning these stuff, I read that you dont have to use removehandler. I think when the object disposes, the handles are automatically removed (only if you added the event with addhandler)
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  6. #6
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Originally posted by MrPolite
    hmmm I think when I was learning these stuff, I read that you dont have to use removehandler. I think when the object disposes, the handles are automatically removed (only if you added the event with addhandler)
    Perhaps you're right. Anyway, the GC will eventually take care of it, but it's still good practice to clean up after yourself.
    Just like at home
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  7. #7
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by pax
    Perhaps you're right. Anyway, the GC will eventually take care of it, but it's still good practice to clean up after yourself.
    Just like at home
    LOL yeah

    umm talking about cleaning event handlers... even though it's not a VB question: in C# when you add an event handler, do you HAVE to remove it before disposing the control? and how the heck do you remove the event?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  8. #8
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    Sorry. I don't know C#. I have tried porting a few small classes to VB with some succes.
    But when starting from scratch, I'm just as blank as the screen...
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  9. #9
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    The opposite of how you added it
    Code:
    //add
    this.button1.Click += new System.EventHandler(this.button1_Click);	
    
    //remove
    this.button1.Click -= new System.EventHandler(this.button1_Click);
    BTW - I'd be willing to bet that the reason the C# forum is slow is because people aren't posting their questions in there.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  10. #10
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by crptcblade
    The opposite of how you added it
    Code:
    //add
    this.button1.Click += new System.EventHandler(this.button1_Click);	
    
    //remove
    this.button1.Click -= new System.EventHandler(this.button1_Click);
    BTW - I'd be willing to bet that the reason the C# forum is slow is because people aren't posting their questions in there.

    and the reason people don't post their C# questions in the C# forum is because the C# forum is slow
    thanks for the reply
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  11. #11
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Actually vegeta's code is not right, you can't remove an event handler like that.
    If you look carefully you are removing a NEW object event handler and that's not what you want. To remove a handler you'll have to when adding the handler, just save the reference in some place and then just do Class.Event -= mySavedHandler

    About this:
    If you continously create and remove objects, and adding handlers to all of them, you will have a lot of "dead" references taking up memory.
    Well the problem isn't that, the problem is that if you run again the same function that created the handlers in the first place, next time an event should happen it will happen twice...
    \m/\m/

  12. #12
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by PT Exorcist
    Actually vegeta's code is not right, you can't remove an event handler like that.
    If you look carefully you are removing a NEW object event handler and that's not what you want. To remove a handler you'll have to when adding the handler, just save the reference in some place and then just do Class.Event -= mySavedHandler
    Hmmm...works fine for me
    Code:
    this.button1.Click += new EventHandler(this.ButtonClickHandler);  //somewhere in the form constructor
    
    
    private void ButtonClickHandler(object sender,EventArgs args)
    {
    	MessageBox.Show("");
    	this.button1.Click -= new EventHandler(this.ButtonClickHandler);
    }
    The messagebox only shows on the first click.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  13. #13
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: [RESOLVED] Dynamic runtime control event handling

    Quote Originally Posted by crptcblade
    You would create a generic sub
    VB Code:
    1. Private Sub MyEventHandler(ByVal sender As Object, ByVal e As EventArgs)
    2. End Sub

    Then use the AddHandler method to attach the control's event to the sub
    VB Code:
    1. Dim b As New Button
    2.  
    3. AddHandler b.Click, New EventHandler(AddressOf MyEventHandler)
    Hey thanks man! That really helped me a lot.

Posting Permissions

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



Click Here to Expand Forum to Full Width