简体   繁体   English

如何在C#中将相同的控件添加到多面板中?

[英]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.

相关问题 如何在C#中在运行时在框架或面板上添加按钮控件 - How add button control on frame or panel at runtime in c# 如何使用 C# 或 cmd 将程序添加到控制面板程序中 - How to add program into Control Panel Programs with C# or cmd 使用autoscroll(c#)将控件添加到面板 - Add control to a panel with autoscroll (c#) WPF C#如何动态地向面板子级添加用户控件,从而使从UC到面板的气泡/隧道 - WPF c# how to add a user control to a panel children dynamically that bubbles/tunnels from UC to Panel C#-带有面板的自定义控件,如何使IDE向面板添加项目? - C# - Custom control with panel on it, how do I make the IDE add items to the panel? 向面板添加控件,并且控件不再显示c# - add control to Panel and control doesn't display anymore c# c#当我将控件添加到面板时,控件变为NULL - c# when I add control to panel, the control becomes NULL 如何在C#中使用面板附加菜单控件 - How to attach menu control with panel in c# C# WinUI 项目,如何在同一个控件上添加多个 ExpressionAnimation? - C# WinUI project, How to add Multiple ExpressionAnimation on same control? 在C#中向面板添加自定义控件时,如何处理创建窗口句柄异常? - How to deal with create window handle exception when add custom control to a panel in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM