# event and eventhandler

## Recommended Posts

Andregolas    122
Hi guys, I'm having trouble at the moment with the following staff:
private class cButton
{
[...]

//Clickfunktion
public event EventHandler OnClick;
}

[...]

cButton Button1 = new cButton();
Button1.OnClick += new EventHandler(Button1Click);

[...]

private void Button1Click(object sender, EventArgs e)
{
Shutdown();
}

So far it's working, but now, I check if the left mousebutton is clicked, and if it is clicked, I wanna call the Button1.OnClick event that the game exits.
Button1.OnClick();

Thats not working because its an event, how can I realize it?

##### Share on other sites
Antheus    2409
You pass the parameters to the OnClick that match the handler's signature, in your case: object sender, EventArgs e.

But generally, you should define a new method in your cButton: performClick() (or something similar), which triggers the event.

The whole reason for events is to de-couple functionality. Triggering an event manually defeats this purpose - you might as well provide a method that calls Shutdown directly.

Your event handler responds to click event (which is abstract term), not to invocation of click event.

##### Share on other sites
Andregolas    122
Okay thanks for your answer, it really helped and I've fixed the error!