public event Action Click; // add this as a member of the button class
protected virtual void OnClick() // Add this function as a member of the button class
{
if (Click != null)
Click(); // This causes all of the attached delegates to be called.
}
// elsewhere, you hook the event using any of the following forms:
// Lambda expression form
button.Click += () => Trace.WriteLine("button clicked!");
// Lambda statement form
button.Click += () =>
{
// Inline code here
};
// Anonymous method form
button.Click += delegate
{
// Inline code here
};
// Standard form
button.Click += FunctionName; // Note: Visual studio will auto-complete this for you if you press TAB twice after typing the +=
and:
void FunctionName()
{
// Stuff here.
}
Events with parameters:
public event Action<MouseButton> Click; // add this as a member of the button class
protected virtual void OnClick(MouseButton mb) // Add this function as a member of the button class
{
if (Click != null)
Click(mb); // This causes all of the attached delegates to be called.
}
// elsewhere, you hook the event using any of the following forms:
// Lambda expression form
button.Click += (mb) => Trace.WriteLine("button clicked: " + mb);
// Lambda statement form
button.Click += (mb) =>
{
// Inline code here
Trace.WriteLine("A button was clicked.");
Trace.WriteLine("It was: "+mb);
};
// Anonymous method form when you don't need the parameter list
button.Click += delegate
{
// Inline code here
};
// Standard form
button.Click += FunctionName; // Note: Visual studio will auto-complete this for you if you press TAB twice after typing the +=
and:
void FunctionName(MouseButton mb)
{
// Stuff here.
}