How to use OnPaint event in C #?

I saw some similar questions on the site, but none of them helped me.

I have a function that draws several lines in a form when a button is pressed, which changes in form depending on the values ​​that the user enters into some text fields.

My problem is that when I minimize the shape, the lines disappear and I realized that this can be solved using the OnPaint event, but I really don’t understand how to do this.

Can someone give me a simple example of using a function to draw something by pressing a button using the OnPaint event?

+5
source share
2 answers

Here you read the Simpe MSDN User Controls Tutorial

Button OnPaint.

:

protected override void OnPaint(PaintEventArgs pe)
{
   // Call the OnPaint method of the base class.
   base.OnPaint(pe);

   // Declare and instantiate a new pen.
   System.Drawing.Pen myPen = new System.Drawing.Pen(Color.Aqua);

   // Draw an aqua rectangle in the rectangle represented by the control.
   pe.Graphics.DrawRectangle(myPen, new Rectangle(this.Location, 
      this.Size));
}

EDIT:

public Color MyFancyTextColor {get;set;} OnPaint. Alsow .

+6

, () , Paint.

, , Paint :

this.Paint += new PaintEventHandler(YourMethod);

YourMethod , .

, , , :

void YourMethod(object sender, PaintEventArgs pea)
{
   // Draw nice Sun and detailed grass
   pea.Graphics.DrawLine(/* here you go */);
}

, , OnPaint. .

+2