37

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?

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
steavy
  • 1,483
  • 6
  • 19
  • 42
  • 2
    since this question is still alive(I even got some reputation for it :) ), I would like to place here a small warning: if you think that you need to do something like this in your code than almost for sure you are doing something wrong. So did I. You probably have some bad design and you`d better spend some time, reconsidering what you are doing and why. – steavy Dec 11 '14 at 11:11
  • I actually think it's a relevant question, I wanted to create a popup menu which generically included all items from a toolbar as well as what the context menu already had, the PerformClick method mentioned by itsme86 helped :-) – s1cart3r Mar 24 '17 at 13:38
  • 2
    In response to the warning, this is actually very relevant for UI testing. You often do need to "trigger a click" without clicking for automated testing. – Heather Oct 05 '18 at 10:37

9 Answers9

50

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

button1.PerformClick();
itsme86
  • 19,266
  • 4
  • 41
  • 57
  • 5
    Generally, you'll just have to call your event handlers manually. e.g.: button1_Shown(button1, EventArgs.Empty); – itsme86 Aug 29 '12 at 19:54
25

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);
    }

}
TomBot
  • 1,126
  • 1
  • 8
  • 13
18

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.

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
6

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);
    }
Tarzan
  • 4,270
  • 8
  • 50
  • 70
  • 2
    I don`t need to call the handlers method. I need to trigger event. Handler is called after triggering event. – steavy Aug 29 '12 at 19:32
4
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 ?

coolmine
  • 4,427
  • 2
  • 33
  • 45
3

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. ;)

IamBatman
  • 975
  • 12
  • 18
0

In most cases you would not need to do that. Simply wrap your functionality in functions related to a specific purpose (task). You call this function inside your event and anywhere else it's needed.

Overthink your approach.

Jonathan
  • 1,955
  • 5
  • 30
  • 50
0

I recently had this problem where I wanted to programatically click a button that had multiple event handlers assigned to it (think UserControl or derived classes).

For example:

myButton.Click += ButtonClicked1
myButton.Click += ButtonClicked2;

void ButtonClicked1(object sender, EventArgs e)
{
    Console.WriteLine("ButtonClicked1");
}

void ButtonClicked2(object sender, EventArgs e)
{
    Console.WriteLine("ButtonClicked1");
}

When you click the button, both functions will get called. In the instances where you want to programmatically fire an event handler for a function from a form (for example, when a user presses enter in a Text field then call the InvokeOnClick method passing through the control you. For example

this.InvokeOnClick(myButton, EventArgs.Empty);

Where this is the Form instance you are in.

Ocean Airdrop
  • 2,793
  • 1
  • 26
  • 31
-3

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

Daniel Kelley
  • 7,579
  • 6
  • 42
  • 50