-
October 13th, 2010, 09:31 AM
#1
Passing extra parameters on EventHandler
Hi, new to the board so go easy
Ok the problem I have is this.
I have 2 combo boxes on a form that share "almost" the same functionality. What I'm wanting to do is when Im setting up the event handlers is some how tell the event which combo box is being used to I can use the same code for both combo boxes, hopefully by the use of extra parameters as this is a slightly simplified example. If that makes sense.
I have this at the moment
cmbProducts.KeyUp += new KeyEventHandler(keypressed);
what I want is something along the lines of this
cmbProducts.KeyUp += new KeyEventHandler(keypressed, cmbProducts);
cmbProducts2.KeyUp += new KeyEventHandler(keypressed, cmbProducts2);
calling
public void keypressed(object sender, KeyEventArgs e, ComboBox cmbtouse)
{
do some combobox giggery pokery...
}
I've spent the last day trawling through the internet now, by the looks of it I need to use some kind of delegate, but im buggered if I can find any straight forward examples.
Could someone *Please* either point me to some resource that will help me or, basically just show me what I need to, I'm fairly sure what I'm needing is relatively simple - I just cant find any examples !!
Any help would be greatly appreciated here guys
Thanks
-
October 13th, 2010, 09:34 AM
#2
Re: Passing extra parameters on EventHandler
Sorry its Visual Studio 2010 c#
-
October 13th, 2010, 11:53 AM
#3
Re: Passing extra parameters on EventHandler
Nope. KeyUp is a KeyEventHandler, i.e., it expects a delegate with a certain signature. How would the control know when to pass these extra parameters and how would you expect to change the code to do so?
You do know that 'sender' is the control which fired the event, right? You can always check that.
-
October 13th, 2010, 01:37 PM
#4
Re: Passing extra parameters on EventHandler
you do know that the sender parameter IS THE OBJECT THAT TRIGGERED THE EVENT.... right?
Sure, it comes over as object type, but you can easily remedy that by casting it as a combo box. Then you can do what ever "combobox giggery pokery..." you need to do.
-tg
-
January 17th, 2013, 06:02 AM
#5
Re: Passing extra parameters on EventHandler
This thread is old, but in case someone still needs an answer to this question (as I did) here is my solution:
Use Tag property. When you subscribe to those events, attach some relevant information to the Tag property and later in your handler just retrieve Tag from sender parameter and take action according to its value.
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
|