Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey,

I' trying to copy one panel into another panel which is created at run time but the old panel keeps disappearing , can you tell me what's wrong ?

my code :

VB
Friend withevent NewPanel As New Panel
Me.Controls.Add(NewPanel)
NewPanel = Panel1
NewPanel.Top=Panel1.Top + Panel1.Height 
Posted

1 solution

You're not really creating a new panel.

What you're doing is creating a new pointer for a Panel called NewPanel

And yes, for a second you're creating a new Panel and assigning it to the pointer. But then you immediately change the pointer for the new panel to point to the old one.

So NewPanel == Panel1

This isn't creating a copy. Objects such as Panels exist in the Stack area of memory.

This mean that NewPanel = Panel1 isn't creating a copy. It's just changing NewPanel so it points to the same memory location as Panel1. NewPanel and Panel1 are the same Panel instance.

To create a copy of the panel you'll have to create a new panel and then configure it using your code. So instead of NewPanel = Panel1 you'd have something similar to this:

C#
NewPanel.Left = Panel1.Left
NewPanel.Top = Panel1.Top


Adding all the properties you need to configure the new panel as the old one was.

You then need to do a similar thing for the all the controls within the panel.

Truth be told, that's a lot of hard work. I'd create a custom control which contains a Panel and all the controls contained within the Panel. Then you need only create a new instance of your custom control.

The following is an introduction to developing custom controls.

http://msdn.microsoft.com/en-us/library/6hws6h2t.aspx[^]
 
Share this answer
 
Comments
ayat abukhadra 26-Sep-12 8:43am    
OK , i got it =D i didn't know that information , thank you =D I'll try to create the custom control =D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900