How can I handle an event for a control that does not yet exist?
Printable View
How can I handle an event for a control that does not yet exist?
You would create a generic sub
VB Code:
Private Sub MyEventHandler(ByVal sender As Object, ByVal e As EventArgs) End Sub
Then use the AddHandler method to attach the control's event to the sub
VB Code:
Dim b As New Button AddHandler b.Click, New EventHandler(AddressOf MyEventHandler)
Thanks a lot for the info! That's exactly what I'm looking for. :afrog:
Hi.
Just remember to use
VB Code:
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)Quote:
Originally posted by pax
Hi.
Just remember to use
VB Code:
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.
Perhaps you're right. Anyway, the GC will eventually take care of it, but it's still good practice to clean up after yourself.Quote:
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)
Just like at home :bigyello:
LOL yeah:DQuote:
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 :bigyello:
umm talking about cleaning event handlers... even though it's not a VB question:D: 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?
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...:bigyello:
The opposite of how you added it
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.Code://add
this.button1.Click += new System.EventHandler(this.button1_Click);
//remove
this.button1.Click -= new System.EventHandler(this.button1_Click);
:afrog:
and the reason people don't post their C# questions in the C# forum is because the C# forum is slow:afrog:Quote:
Originally posted by crptcblade
The opposite of how you added it
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.Code://add
this.button1.Click += new System.EventHandler(this.button1_Click);
//remove
this.button1.Click -= new System.EventHandler(this.button1_Click);
:afrog:
thanks for the reply:D
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: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...:sick:Quote:
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...works fine for meQuote:
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
The messagebox only shows on the first click.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);
}
Hey thanks man! That really helped me a lot.Quote:
Originally Posted by crptcblade