Passing extra parameters on EventHandler
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Passing extra parameters on EventHandler

Hybrid View

  1. #1
    Join Date
    Oct 2010
    Posts
    5

    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

  2. #2
    Join Date
    Oct 2010
    Posts
    5

    Re: Passing extra parameters on EventHandler

    Sorry its Visual Studio 2010 c#

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  4. #4
    Join Date
    Dec 2007
    Posts
    234

    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
    * I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help - how to remove eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
    * On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
    MVP '06-'10

  5. #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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center