Sign in to follow this  
Andregolas

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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this