[英]How to add the same control into multi panel in C#?
I have a button called button1 and two panels called: panelA and panelB (visible is false by default) and the following code (WinForms):我有一个名为 button1 的按钮和两个名为的面板:panelA 和 panelB(默认情况下可见为 false)以及以下代码(WinForms):
panelA.Controls.Add(button1);
panelB.Controls.Add(button1);
panelB.Visible = true; // I see the button1
panelA.Visible = true; // I don't (ofcoz panelB.Visible is still false)
MessageBox.Show(panelA.Controls.Contains(button1).ToString); //False, why?
I don't know why?我不知道为什么? Maybe it's a stupid question for you but I'm a newbie so I don't really have any idea about this problem?
也许这对你来说是一个愚蠢的问题,但我是一个新手,所以我对这个问题一无所知? Can you help me?
你能帮助我吗? Thanks!
谢谢!
The object button1
can have only one visual parent.对象
button1
只能有一个视觉父对象。 Therefore you shouldn't add it to 2 different parents.因此,您不应将其添加到 2 个不同的父项。
So, you need to have 2 button objects.因此,您需要有 2 个按钮对象。
I don't know why your seccond button are not visible.我不知道为什么你的第二个按钮不可见。 But, Why not to use two differents buttons with the same click event?
但是,为什么不使用具有相同点击事件的两个不同按钮呢?
Have you tried if the problem is still there is you try to add two different instances of a button?如果问题仍然存在,您是否尝试过尝试添加按钮的两个不同实例?
Good Luck.祝你好运。
Only One Instance of anobject can be shown , So you have to create another Instance foryour button.只能显示一个对象的一个实例,因此您必须为您的按钮创建另一个实例。 Both of them will act the same ( because they are One Control - but will have different acts in order to have different instance ).
他们都将采取相同的行动(因为它们是一个控制 - 但为了拥有不同的实例而将采取不同的行动)。
And this is because you only can have One instance of a control.这是因为您只能拥有一个控件实例。 you really do not need same instance of an object.
你真的不需要一个对象的相同实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.