0

I know this question has been asked many times, but I read the answers for other questions and my code still gives me an error

This is my code

Button loadButton = new Button();
                loadButton.Text = "Load";
                loadButton.Click += new EventHandler(this.openOutlook());

private void openOutlook()
        {}

now I got error states method names expected on this line new EventHandler(this.openOutlook());

I tried to add object sender, EventsArg arg to the header of the method.

2
  • possible duplicate of How to call Button OnCLick eventhandler in code behind with a value? Commented Apr 6, 2015 at 10:02
  • You have 2 problems: 1) Your openOutlook method does not have the signature of openOutlook(object sender, EventArgs e) and when you invoke the method in your event handler, you need to ditch the brackets, since you are passing a function pointer (delegate). Commented Apr 6, 2015 at 10:04

3 Answers 3

1

You don't need to explicitly new up the EventHandler - if the target handler method has the appropriate signature, you can add the subscription directly:

loadButton.Click += openOutlook;

private void openOutlook(object sender, EventArgs eventArgs)
{
    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this,

Button loadButton = new Button();
loadButton.Text = "Load";
loadButton.Click += new EventHandler(btnOk_Click);
void btnOk_Click(object sender, EventArgs e)
{

}

When intellisence showing the click event name on button object, press the tab key twice if ur using Visual Studio IDE.

Comments

0

Try this:

new EventHandler(this.openOutlook);

without parenthesis. Your event handler expects just method name.

and for your method

openOutlook(object sender, EventArgs eventArgs)

2 Comments

method also required object sender, EventsArg arg parameters.
Please check question as accepted if it helped you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.