Declaring Custom Events in User Controls
Posted on April 15, 2007 at 11:31I am constantly needing to search my code to find the exact syntax for how to do this, so for future reference (for myself and anyone else who finds this useful), to declare and call an event from a custom user control or inherited control:
public class CustomEventArgs : EventArgs {
public CustomEventArgs(int propValue) {
_propValue = propValue;
}
private int _propValue;
public int PropValue {
get { return _propValue; }
}
}
public delegate void CustomEventHandler(object sender, CustomEventArgs e);
public event CustomEventHandler CustomEvent;
public void Function() {
// Do Stuff
if (CustomEvent != null) {
CustomEventArgs e = new CustomEventArgs(x);
CustomEvent(this, e);
}
}
If you don’t need a CustomEventArgs class, just use the built-in EventArgs instead.