Im using WinForm C# Have MainForm there is one panel where. my Inventory and Sell user controls are opening in panel. panel1.Controls.Add(inventory); How to check if userControls are open? When i check it i want to add tabControl. But i dont know how to add in tabPage controls without closing user control. Thanks
2 Answers
I mean if user control is already added in panel1.Controls. If its added gave name of user control
– Acid
How could the user control possibly be added to panel1.Controls
without you knowing it? And if you added it yourself, you should already know the name of the user control.
Thus, all you have to do is loop through the controls in panel1.Controls
and see if you find your user control. For example:
foreach (Control ctrl in panel1.Controls)
{
if (ctrl.Name == myUserControl)
{
// Found the control!
// (do something here...)
}
}
Alternatively, if you for whatever reason don't know the name of the control, you could still find all the controls of type UserControl
that have been added to the panel's Controls collection. Like so:
foreach (Control ctrl in panel1.Controls)
{
if (ctrl is UserControl)
{
// Found a UserControl!
// (do something here...)
}
}
Remember that the Tag
property provided on every control gives you a way to uniquely identify it. You can check that property for matches, too, if you don't know the name.