0

On a C#-Form I have a button. When I double-click the button then it automatically creates the function

 private void button_Click(object sender, EventArgs e).

This works fine.

My question is: How can I manually execute this function? I want to run in once in the constructor?

2
  • 1
    Beware that it's usually a bad approach, because you can't easily distinguish between "real" clicks and "fake" clicks. Usually you create a method which will do the Action and call it from Button_Click. In this way you can call the method whenever you want without faking the click. Commented Feb 21, 2014 at 8:17
  • Why do you want to execute the Click handler in the first place? This handler should only contain enough code to call the proper business functions, not contain the business logic itself. Extract the code to another function and call that function instead Commented Feb 21, 2014 at 9:14

6 Answers 6

3

My question is: How can i manually execute this function?

Do not execute button click manually. It's purpose to be executed only when button raises click event.

I want to run in once in the constructor?

Extract code which you have in this event handler to separate method and run it both from c constructor and handler:

public Form1()
{
    InitializeComponent();
    DoSomething();
}

private void button_Click(object sender, EventArgs e)
{
    DoSomething();
}

private void DoSomething()
{
    // extract handler code here
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can try this:

button_Click(button,new EventArgs());

or you can do

button.PerformClick();

7 Comments

Both will works. But, what happen when sender object is used in button click event on first method?
This is an example of bad code and bad architecture. The good answer is the @sergey one
I don't think so @RickyAH. the question is "How to execute button_click manually?" not "how to call a function?"
Sorry to disagree. The question is bad formed as @Thomas wants to perform the same action as the handler, and not programmatically trigger the event. I know it for sure because he wants to call the event handler code in the constructor Makes no sense to fake a button click event when the controls are not even drawn ;)
I know Patrick calling the event manually is the bad idea and it is our responsibility to teach proper method. But, when someone really want to do this then it should be proper.
|
0
button.PerformClick();

See MSDN

Comments

0

Its better if you transfer the code from button_click to new function, and call that one. Like this:



    void MyFunc()
    {
        //code that was previously in button_click
    }
    private void button_Click(object sender, EventArgs e)
    {
        MyFunc();
    }

    void OtherFunction()
    {
        MyFunc();
    }

Comments

-1

That function is an event handler: a function that gets executed as a response to an event, in this case a user event: the user clicked a button.

If you need to execute the very same action without being triggered with an event, the correct approach is move the code that performs the action to a separate method

private void ExecuteMyAction()
{
   // Do stuff
}

private void button_Click(object sender, EventArgs e)
{ 
   ExecuteMyAction()
}

And then you can call safely that method in your constructor

public class MyClass
{ 
   public MyClass()
   {
      ExecuteMyAction();
   }
}

Actually it is a better to keep the code in a handler as lean as possible and move the code that performs that action to another method. In the handler you just need to gather the information needed to perform an action that it is only available when the event is processed and then pass that information to another part of the system that is in charge of processing that information.

For example, if you need the user to input a name in a Textbox when the user clicks a button, the button_click handler should only get the string from the text box and call a method that saves that information, passing the string as a parameter.

That way you reduce coupling by keeping separated the code that performs the action (business code) from the code that commands the action to be performed (which is boilerplate code that depends on the framework you use)

Comments

-2

You can call it like this:

button_Click(this, EventArgs.Empty);

2 Comments

Assigning a parent object in parameter for sender instead of button is it right way?
@Nimesh: I think this is a bad idea, since you should separate logic and UI as much as possible.

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.