Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

controls

panel

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!

like image 757
A New Chicken Avatar asked Feb 24 '10 10:02

A New Chicken


2 Answers

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.

like image 189
Vlad Avatar answered Sep 22 '22 16:09

Vlad


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.

like image 32
Jonathan Avatar answered Sep 24 '22 16:09

Jonathan