How to add the same control into multi panel in C#?

扶醉桌前 提交于 2019-12-04 03:28:52

问题


I have a button called button1 and two panels called: panelA and panelB (visible is false by default) and the following code (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!


回答1:


The object button1 can have only one visual parent. Therefore you shouldn't add it to 2 different parents.

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.




回答3:


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.



来源:https://stackoverflow.com/questions/2325397/how-to-add-the-same-control-into-multi-panel-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!