How to copy panels in C#

Hi Experts,

I need some help from you.

I am creating one application in which i want to create bunch og control in a panel and that same panel I want to copy not Move to some other location
When I copy then there will be two panel but unfortunately there is only one panel with different location.

Please provide me solution ASAP.

Who is Participating?
This function can be called at any point in your codebehind. It will accept a panel that needs to be copied, and a placeholder in the location that you want the copy of the control to appear. Any questions?

private void CopyPanel(Panel OrigPanel, PlaceHolder PlaceHolderOnScreen)   //The original panel is the one to be copied, the PlaceHolder is where the copy should
                                                                                                              //appear on the screen
                  Panel PanelCopy = OrigPanel;
                  PanelCopy.ID = OrigPanel.ID + "Copy";  //Keep the original ID, but append "Copy"

                  foreach(Control ControlCopy in OrigPanel.Controls)  //Loop through every control in the OriginalPanel
                        Control tempCopy = ControlCopy;                  //Create a copy of the control from the Original Panel
                        tempCopy.ID = ControlCopy.ID + "Copy";       //Keep the original ID, but append "Copy"

                        PanelCopy.Controls.Add(tempCopy);             //Add to the Copy of the original Panel

            PlaceHolderOnScreen.Controls.Add(PanelCopy);                //Add Panel to the Placeholder to display where you want it on the screen
What you need to do is Create a new panel where you want the "copy" to be. Then you do a loop to copy the other controls i.e. (this is somewhat pseudocode)

//Create new panel in specific location

ForEach Control in OriginalPanel.GetControls()
    ControlCopy = Control;
    ControlCopy.ID = Control.ID + "Copy";


This should create copies of all the controls into the new panel and keep their original ID's, but append "Copy" to then end. The reason you have to append this is because you cannot have two controls of the same name in one form.
avinash_takaleAuthor Commented:
thanks a lot

but can you explain if panel one contains one text box and i want to create another panel i.e. panel2 at runtime with same content as panel 1.
can give me some more detail regarding how to code
give me code its reallly urgent.

thanks in advance
avinash_takaleAuthor Commented:
sorry to say but the PlaceHolder control works with Webform and not with windows form
so can you tell me other alternative.

Really helpful this code for me.

Sorry about that, in that case you can use the Position property of the Panel. This will specify the X,Y coordinates of where to place the Panel on the screen and you can drop out all occurences of the placeholder from my code.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.