|
-
May 11th, 2004, 02:13 PM
#1
Thread Starter
Lively Member
[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.
-
May 11th, 2004, 02:33 PM
#2
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)
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
May 11th, 2004, 03:09 PM
#3
Thread Starter
Lively Member
Thanks a lot for the info! That's exactly what I'm looking for.
-
May 11th, 2004, 03:46 PM
#4
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.
I wish I could think of something witty to put in my sig...
...Currently using VS2013...
-
May 11th, 2004, 06:36 PM
#5
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.
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!!
-
May 12th, 2004, 03:40 AM
#6
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...
-
May 12th, 2004, 11:59 AM
#7
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!!
-
May 12th, 2004, 12:02 PM
#8
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...
-
May 12th, 2004, 12:05 PM
#9
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
-
May 12th, 2004, 12:30 PM
#10
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!!
-
May 12th, 2004, 05:27 PM
#11
yay gay
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/
-
May 12th, 2004, 05:35 PM
#12
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
-
May 26th, 2006, 12:20 AM
#13
Hyperactive Member
Re: [RESOLVED] Dynamic runtime control event handling
 Originally Posted by crptcblade
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)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|