How to add the same control to multiple panels in C #?

I have a button named button1 and two panels: panelA and panelB (the default is false) 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 do not know why? Maybe this is a stupid question for you, but I'm a newbie, so I really don't know about this problem? Could you help me? Thanks!

+4
source share
3 answers

button1 can have only one visual parent. Therefore, you should not add it to two parents.

So you need to have 2 buttons.

+5
source

I do not know why your seccond button is not visible. But why not use two different buttons with the same click event?

Have you tried, if the problem is still there, are you trying to add two different button instances?

Good luck.

+1
source

Only one instance of anobject can be shown, so you need to create another Instance foryour button. Both will act the same way (because they are one control, but will have different actions to have a different instance).

And this is because you can only have one instance of a control. you really don't need the same instance of the object.

0
source

Source: https://habr.com/ru/post/1302221/


All Articles