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);
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 !!
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.
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.