y_zyx
y_zyx

Reputation: 642

Panel clear everything

I would like to reset a panel to its initial state. For example, I set an image as background, and I draw graphics on a part of the panel. I have to clear everything. How?

Upvotes: 13

Views: 67495

Answers (5)

Filippe
Filippe

Reputation: 31

This was the only solution that worked for me:

private void button3_Click(object sender, EventArgs e) // Clear button
{
    using (g = Graphics.FromImage(bmp))
    {
        g.Clear(Color.Transparent); // You can choose another color
                                    // for your background here.
        panel1.Invalidate();
    }
}

Upvotes: 1

You have to clear the panel first:

panel1.Controls.Clear();

Then call the initial form:

panel1.Controls.Add(orig_form);

Upvotes: 25

stdntuvmath
stdntuvmath

Reputation: 65

This worked for me:

private void button1_Click(object sender, EventArgs e)//clear Data
{
   panel1.Controls.Clear();            
   this.Refresh();
}

Upvotes: 0

Salman
Salman

Reputation: 1380

Use the following code to delete all graphics from the panel

panel1.Invalidate();

If there is something you need to add to panel's initial state then after you call invalidate you again have to set those things.

If the initial state of panels needs some graphics or data you can put that in panel's graphics event, so everytime invalidate is called your panel get the initial state with those items.

Upvotes: 5

Jakob
Jakob

Reputation: 39

Use the panel1.refresh(); command. It resets the panel to its initial state.

Upvotes: 3

Related Questions