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.

Thanks
-Avinash
avinash_takaleAsked:
Who is Participating?
 
strickddCommented:
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
            }
0
 
strickddCommented:
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";

     PanelCopy.Controls.Add(ControlCopy);
Next


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.
0
 
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
please........
give me code its reallly urgent.

thanks in advance
0
 
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.

-Avinash
0
 
strickddCommented:
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.
0
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.