Creating Rectangle-based buttons with OnClick events

Posted by Djentleman on Game Development See other posts from Game Development or by Djentleman
Published on 2013-01-01T04:00:28Z Indexed on 2013/10/25 16:13 UTC
Read the original article Hit count: 272

Filed under:
|
|
|

As the title implies, I want a Button class with an OnClick event handler. It should fire off connected events when it is clicked.

This is as far as I've made it:

public class Button {

    public event EventHandler OnClick;

    public Rectangle Rec { get; set; }
    public string Text { get; set; }

    public Button(Rectangle rec, string text) {
        this.Rec = rec;
        this.Text = text;
    }

}

I have no clue what I'm doing with regards to events. I know how to use them but creating them myself is another matter entirely. I've also made buttons without using events that work on a case-by-case basis.

So basically, I want to be able to attach methods to the OnClick EventHandler that will fire when the Button is clicked (i.e., the mouse intersects Rec and the left mouse button is clicked).

© Game Development or respective owner

Related posts about c#

Related posts about xna-4.0