[Last Call] Learn how to a build a cloud-first strategyRegister Now


How to copy panels in C#

Posted on 2006-05-22
Medium Priority
Last Modified: 2012-05-05
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.

Question by:avinash_takale
  • 3
  • 2
LVL 28

Expert Comment

ID: 16737175
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.

Author Comment

ID: 16737343
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
LVL 28

Accepted Solution

strickdd earned 1000 total points
ID: 16737794
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

Author Comment

ID: 16738609
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.

LVL 28

Expert Comment

ID: 16738643
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.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month18 days, 2 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question