Creating an EventHandler that takes a function as a parameter - WHEN THE FUNCTION ITSELF HAS PARAMETERS

Posted on 2006-05-23
Last Modified: 2010-07-27

I need to implement an EventHandler in C# that takes a function as a parameter - and where the function itself has parameters.

So far, I've found lots of stuff on EventArgs and delegates, but I just haven't been able to quite put this together.

What I have is a combobox that I want to have trigger an event when it is clicked.

    System.Windows.Forms.ComboBox comboBox = new ComboBox();

    string str = "A specific value";  //How do I pass this as a parameter to comboBoxOutputs_Clicked?

    comboBox.Click += new System.EventHandler(comboBoxOutputs_Clicked);

    //Here's the function;  I want to set the comboBox's text to "A specific value"
    private void comboBoxOutputs_Clicked(object sender, System.EventArgs e)
        System.Windows.Form.ComboBox comboBox = (ComboBox)sender;

        comboBox.Text = <I want the specific value here that can be passed in as a variable>;

I've looked at a lot of stuff on EventArgs and such, but haven't been able to come up with something workable so far.

Thanks in advance,

Question by:tmitchell68
    LVL 6

    Accepted Solution

    You can't really do that because the comboBoxOutputs_Clicked method must match the delegate that the combobox is expecting, so you really can't add any parameters. There might be a better design solution to this, but if you need a solution like you're after you might have to do something like:

    string str = "a value";
    comboBox.Tag = str;
    comboBox.Click += new EventHandler(comboBoxOutputs_Clicked);

    private void comboBoxOutputs_Clicked(object sender, System.EventArgs e)
        ComboBox comboBox = (ComboBox)sender;
        comboBox.Text = (string)comboBox.Tag;
    LVL 25

    Assisted Solution

    You can't do this with the default event handler, since you don;t have control over the EventArgs; that gets created by the ComboBox.  

    Where are you getting the value that you want to be shown in the ComboBox?  Can it be accessed from within the event handler?
    LVL 6

    Assisted Solution

    comboBoxOutputs must match the delegate that the Click event on the combobox is expecting*

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Suggested Solutions

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now