本文介绍了如何在C#中将相同的控件添加到多面板中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为button1的按钮,两个面板分别为:panelA和panelB(默认情况下,visible为false)和以下代码(WinForms):
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!
推荐答案
对象button1
只能具有一个可视父对象.因此,您不应将其添加到2个不同的父母中.
The object button1
can have only one visual parent. Therefore you shouldn't add it to 2 different parents.
因此,您需要有2个按钮对象.
So, you need to have 2 button objects.
这篇关于如何在C#中将相同的控件添加到多面板中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!