There is nothing special about it. The base class that defines the Paint
event contains code morally equivalent to this:
protected virtual void OnPaint(PaintEventArgs e)
{
var paintHandlers = this.Paint;
if (paintHandlers != null)
{
paintHandlers(this, e);
}
}
OnPaint
serves two functions:
- Raises the
Paint
event so external subscribers can be notified (that's how a hypothetical form1_Paint
will eventually get called).
- Lets derived classes directly respond to paint events without needing to involve an event handler -- simply override the method (but the derived class must not forget to call the base implementation!).
When the time comes to raise the Paint
event, some other code in the base class creates a PaintEventArgs
instance based on information at hand and calls OnPaint
:
// ...somewhere in the class...
OnPaint(new PaintEventArgs(...));
In the special case where the event argument is typed as EventArgs
there is no need to create a new instance, you can directly pass the EventArgs.Empty
static member to the OnSomething
method.
As to the when this happens: whenever a message is drawn from the application's message pump and processing it indicates that the event should be raised. You might also want to read Understanding events and event handlers in C#.