开发者

Check if user control is already open

开发者 https://www.devze.com 2023-02-21 20:12 出处:网络
Im using WinForm C# Have MainForm there is one panel where. my Inventory and Sell user controls are opening in panel. panel1.C开发者_运维知识库ontrols.Add(inventory);

Im using WinForm C# Have MainForm there is one panel where. my Inventory and Sell user controls are opening in panel. panel1.C开发者_运维知识库ontrols.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


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.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消