panel clear everything

爱⌒轻易说出口 提交于 2021-01-14 14:08:08

问题


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


回答1:


You have to clear the panel first

panel1.Controls.Clear();

then call the initial form.

panel1.Controls.Add(orig_form);



回答2:


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.




回答3:


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




回答4:


That is 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();
        }
    }



回答5:


Here is a SO link that looks to do exactly what you want:

Resetting a winform's elements to initialized state (C#/.Net)



来源:https://stackoverflow.com/questions/5959069/panel-clear-everything

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!