0

I'm learn various features of C# and I am trying to create a Windows Forms program in which you draw a rectangle.

I understand that onpaint method supposed to override the paint method and overriding helps if you wish to provide a method to the parent class.

In what cases should you use the OnPaint handler

protected override void OnPaint(PaintEventArgs e)

instead of the regular paint handler

private void Form1_Paint(object sender, PaintEventArgs e)

In another way of asking is what methods do the onpaint method provide that the form1_paint couldn't?

edsun
  • 15
  • 4
  • @Matthew `OnPaint` is not an event, it's a protected method that raises the `Paint` event. You have to [have a protected method](https://learn.microsoft.com/en-us/dotnet/standard/events/#events) that raises the event because otherwise it would be impossible to raise the event from derived classes because events are not inherited. – GSerg Feb 20 '19 at 18:55
  • Oh yea, that makes sense from the names. – Matthew Feb 20 '19 at 18:59

1 Answers1

-2

According to C# Winforms - Paint method questions pointed out by Broots, both methods work without much advantage over another. According to Thomas, overriding OnPaint may slightly be faster because OnPaint do not to fire the Paint events.

edsun
  • 15
  • 4