Trigger control's event programmatically

2019-01-22 18:28发布

问题:

Assume that I have a WinFoms project. There is just one button (e.g. button1).

The question is: is it possible to trigger the ButtonClicked event via code without really clicking it?

回答1:

Button controls have a PerformClick() method that you can call.

button1.PerformClick();


回答2:

The .NET framework uses a pattern where for every event X there is a method protected void OnX(EventArgs e) {} that raises event X. See this Msdn article. To raise an event from outside the declaring class you will have to derive the class and add a public wrapper method. In the case of Button it would look like this:

class MyButton : System.Windows.Forms.Button
{

    public void ProgrammaticClick(EventArgs e)
    {
        base.OnClick(e);
    }

}


回答3:

You can just call the event handler function directly and specify null for the sender and EventArgs.Empty for the arguments.

void ButtonClicked(object sender, EventArgs e)
{
    // do stuff
}

// Somewhere else in your code:
button1.Click += new EventHandler(ButtonClicked);

// call the event handler directly:
ButtonClicked(button1, EventArgs.Empty);

Or, rather, you'd move the logic out of the ButtonClicked event into its own function, and then your event handler and the other code you have would in turn call the new function.

void StuffThatHappensOnButtonClick()
{
    // do stuff
}

void ButtonClicked(object sender, EventArgs e)
{
    StuffThatHappensOnButtonClick();
}

// Somewhere else in your code:
button1.Click += new EventHandler(ButtonClicked);

// Simulate the button click:
StuffThatHappensOnButtonClick();

The latter method has the advantage of letting you separate your business and UI logic. You really should never have any business logic in your control event handlers.



回答4:

Yes, just call the method the way you would call any other. For example:

    private void btnSayHello_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Hello World!");
    }

    private void btnTriggerHello_Click(object sender, EventArgs e)
    {
        btnSayHello_Click(null, null);
    }


回答5:

button1.PerformClick();

But if you have to do something like this maybe it's better to move the code you have under the event on a new method ?



回答6:

Why don't you just put your event code into a Method. Then have the Event execute the method. This way if you need to execute the same code that the Event rises, you can, but simply just calling the "Method".

void Event_Method()
{
    //Put Event code here.
    MessageBox.Show("Hello!");
}

void _btnSend_Click(object sender, EventArgs e)
{
    Event_Method();
}

void AnotherMethod()
{
    Event_Method();
}

Make sense? Now the "Click" event AND anywhere in code you can trigger the same code as the "Click" event.

Don't trigger the event, call the method that the event calls. ;)



回答7:

use a for loop to call the button_click event

private void btnadd_Click(object sender, RoutedEventArgs e)
{ 
    for (int i = 0; i <= 2; i++)
        StuffThatHappensOnButtonClick(); 
}


void StuffThatHappensOnButtonClick()
{
    ........do stuff
}

we assume at least one time you need click the button