I want to create some engine for monogame in c#. My engine have to include button. I want to do it with events. Basically I have it done, but I want to improve it and there began my problems.
class Button : Component
{
....
public event EventHandler<EventArgs> Click;
public void OnClick()
{
if (MousePressed() && Click != null)
{
Click(this, new EventArgs());
}
}
}
and when I want to add Click event I have to do it in another class:
_buttnon.Click += new EventHandler<EventArgs>(Method);
_buttnon.OnClick();
It works nice, but I want to do it in following way:
_button.RegisterClickEvent(Method);
so I added to the Button class code below:
public void RegisterClickEvent(XXX method) {
Click += new EventHandler<EventArgs>(method);
}
I absolutely have no idea what I have to replace the XXX. I tried:
public void RegisterClickEvent(void(object, EventArgs)method) {
Click += new EventHandler<EventArgs>(method);
}
but it is not working. I tried to search for something on Google, but I do not know what I look for.
Simple use a delegate:
public void RegisterClickEvent(EventHandler<EventArgs> method)
{
Click += new EventHandler<EventArgs>(method);
}
or
public void RegisterClickEvent(EventHandler method)
{
Click += new EventHandler<EventArgs>(method);
}
This works (at least in VS2015, perhaps there is another way in previous (before 2010, since there were some changes with delegates).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments