Click here to Skip to main content
15,908,675 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i create drawing on panel .. if i minimize and maximize the form, drawing will be erased

how can i prevent clear drawing

i test flush(), and dispose()

-----------
C#
private void button1_Click(object sender, EventArgs e)
{
     priceValue();
     Diagrams f2 = new Diagrams();
     f2.Show();
     Graphics g1 = f2.panel1.CreateGraphics();
     g1.Clear(f2.panel1.BackColor);
     Pen pen1 = new Pen(Color.Black,1);
     g1.DrawLine(pen1, 100, 400, 600, 400);
     g1.DrawLine(pen1, 100, 50, 100, 400);
     Font myFont = new System.Drawing.Font("Ubuntu_Titling_Rg", 10, FontStyle.Bold);
     Brush myBrush = new SolidBrush(System.Drawing.Color.Gold);
     int i=0;
     do
     {
         g1.DrawLine(pen1, 95, 50 + i, 100, 50 + i);
         i += 70;
     }
     while (i <= 350);
     System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical);
     g1.DrawString(comboBox1.Text + " items cost schedule", myFont, myBrush, 15, 120, drawFormat);
     g1.DrawString("0", myFont, myBrush, 70, 390);
     g1.DrawString(lastPrice.ToString(), myFont, myBrush, 65, 320);
     g1.DrawString(midP3rice.ToString(), myFont, myBrush, 60, 250);
     g1.DrawString(midPrice.ToString(), myFont, myBrush, 57, 180);
     g1.DrawString(mid2Price.ToString(), myFont, myBrush, 50, 110);
     g1.DrawString(firstPrice.ToString(), myFont, myBrush, 50, 40);
     Pen pen2 = new Pen(Color.FromArgb(30,60,153), 5);
     Rectangle myRectangle = new Rectangle(5, 5, 658, 540);
     g1.DrawRectangle(pen2, myRectangle);
     RectangleF rect = new RectangleF(140, 110, 20, 290);
     g1.DrawString("Main Items", myFont, myBrush, 300, 500);
     int pos = 160;
     for (int j = 0; j < treeView1.Nodes.Count; j++)
     {
         if(treeView1.Parent.HasChildren == true)
             g1.DrawString(treeView1.Nodes[j].Text, myFont, myBrush, pos, 400, drawFormat);
         pos += 460/4;
     }
     g1.Save();
     g1.Dispose();
}
Posted
Updated 2-May-11 5:20am
v4
Comments
Olivier Levrey 2-May-11 9:28am    
I moved your code from comments to question to make it more readable.
Sendian 2-May-11 9:31am    
Thank you

Handle the Paint event for your panel, and put all your drawing code inside that handler.

public Form1()
{
     InitializeComponent();

     //handle the Paint event for the panel
     panel1.Paint += (sender, e) =>
     {
          //put you drawing code here
          //and use e.Graphics instead of CreateGraphics
     };
}


Or without lambda expressions:

public Form1()
{
     InitializeComponent();
     //handle the Paint event for the panel
     panel1.Paint += panel1_Paint;
}

//the painting function for panel1
private void panel1_Paint(object sender, PaintEventArgs e)
{
    //put you drawing code here
    //and use e.Graphics instead of CreateGraphics
    e.Graphics.DrawString(...);
    e.Graphics.DrawRectangle(...);
}
 
Share this answer
 
v4
Comments
RaviRanjanKr 2-May-11 8:41am    
Nice Answer! My 5 :)
Olivier Levrey 2-May-11 8:46am    
Thank you RaviRanjankr.
Sendian 2-May-11 9:23am    
but how can i do with 2 forms
Olivier Levrey 2-May-11 9:32am    
It doesn't change much. I updated my answer.
Sendian 2-May-11 9:42am    
please give me sometime .. what is this mean .. for me is this wrong code i'm begginer
Addition to the answer by Olivier:

If lambda cannot be used (because of v.2.0), use anonymous without lambda. It will have almost all the benefits of lambda, only parameters type should be explicitly specified:

C#
panel1.Paint += delegate(object sender, System.EventArgs eventArgs)
{
    //... same thing...
};


—SA
 
Share this answer
 
Comments
Olivier Levrey 2-May-11 9:59am    
I see. I started using lambda and anonymous methods at the same time with 3.5 and didn't know much about their "history". Have a 5.
Sergey Alexandrovich Kryukov 2-May-11 10:16am    
Thank you, Olivier. I understand. Well, some history from 2.0 is good to know, as 2.0 still makes sense. I provided build for all my articles starting with v.2.0 and up, some small pieces required "#if...".
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900