1
votes

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
It's totally unclear to me what you want to achieve. Can you please redefine your question or explain step by step of what you are trying to do?HABJAN
Yes, please try to explain yourself better... for example what is an opened usercontrol ?digEmAll
I mean if user control is already added in panel1.Controls. If its added gave name of user controlIrakli Lekishvili

2 Answers

4
votes

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.

0
votes

Not sure what you mean by open, but you can handle the ControlAdded event on the Panel class to capture when a control is added...

panel1.ControlAdded += new ControlEventHandler(p_ControlAdded);