Hi!

My problem is about drawing line. In my form, I have a button and if I click the button some data is taken from a database (coordinates). Based on this coordinates, I want to draw lines on a panel. Under button_click event I wrote this code:

Pen myPen = new System.Drawing.Pen(Color.Black, 1);
                Graphics formGraphics;
                formGraphics = panel1.CreateGraphics();
                formGraphics.DrawLine(myPen, x1, y1, x2, y2);
                
                myPen.Dispose();
                formGraphics.Dispose();

I can draw the lines with this code, but if I minimize the form and open again, all lines disappear. The form is redraw again. I put some pictures also on panel. Pictures doesn't disappear.

I know I have to use panel_paint event but I have no idea how I can use panel_paint event and button_click event.

Can you give a small example about it.

Thanks.

Welcome to Daniweb mgroses! Please use code tags when sharing code on the site:

[code=csharp] ...code here....

[/code]

The reason your changes are lost is because when you paint a form it retains the settings until an area of the form or control is invalidated which causes the control to redraw itself. You need to store the coordinates in memory after you retrieve them from the database and leave the code in the form's draw event. In order to test the line disappearing you don't even need to minimize it, just make a call to this.Invalidate(); from somewhere in your form class (like on a button's click event).

Comments
Good explanation!
This article has been dead for over six months. Start a new discussion instead.