将面板复制到在运行时创建的另一个面板 [英] copy a panel into another panel created at runtime
问题描述
嘿,
我试图将一个面板复制到在运行时创建的另一个面板,但是旧面板不断消失,您能告诉我什么地方出问题吗?
我的代码:
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 :
Friend withevent NewPanel As New Panel
Me.Controls.Add(NewPanel)
NewPanel = Panel1
NewPanel.Top=Panel1.Top + Panel1.Height
推荐答案
您并没有真正创建新的面板.
您正在做的是为名为NewPanel
的面板创建一个新的指针
是的,有一秒钟,您正在创建一个新的面板并将其分配给指针.但是随后您立即将新面板的指针更改为指向旧面板的指针.
所以NewPanel == Panel1
这不是创建副本.诸如Panel之类的对象存在于内存的Stack
区域中.
这意味着NewPanel = Panel1
不会创建副本.它只是更改NewPanel,所以它指向与Panel1相同的内存位置. NewPanel和Panel1是相同的Panel实例.
要创建面板的副本,您将必须创建一个新面板,然后使用您的代码对其进行配置.因此,您将拥有与NewPanel = Panel1
相似的东西:
You''re not really creating a new panel.
What you''re doing is creating a new pointer for a Panel calledNewPanel
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.
SoNewPanel == Panel1
This isn''t creating a copy. Objects such as Panels exist in theStack
area of memory.
This mean thatNewPanel = 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 ofNewPanel = Panel1
you''d have something similar to this:
NewPanel.Left = Panel1.Left
NewPanel.Top = Panel1.Top
添加与配置旧面板一样需要配置新面板的所有属性.
然后,您需要对面板中的所有控件执行类似的操作.
实话实说,这是很多艰苦的工作.我将创建一个自定义控件,其中包含一个面板以及该面板中包含的所有控件.然后,您只需要创建自定义控件的新实例.
以下是开发自定义控件的介绍.
http://msdn.microsoft.com/en-us/library/6hws6h2t.aspx [ ^ ]
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[^]
这篇关于将面板复制到在运行时创建的另一个面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!