检查用户控制是否已经打开

发布于 2024-10-29 06:31:48 字数 151 浏览 2 评论 0原文

我正在使用winform c# 有主化有一个面板。我的库存和销售用户控件正在面板中打开。面板1.controls.add(库存); 如何检查UserControls是否打开? 当我检查它时,我想添加TabControl。但是我不知道如何在不关闭用户控件的情况下添加TABPAGE控件。谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不及他 2024-11-05 06:31:48

我的意思是如果用户控件已添加到 panel1.Controls 中。如果添加了用户控件的名称
– 酸

如何在您不知情的情况下将用户控件添加到 panel1.Controls 中?而且如果你自己添加的话,你应该已经知道用户控件的名称了。

因此,您所要做的就是循环访问 panel1.Controls 中的控件,看看是否找到您的用户控件。例如:

foreach (Control ctrl in panel1.Controls)
{
    if (ctrl.Name == myUserControl)
    {
        // Found the control!
        // (do something here...)
    }
}

或者,如果您出于某种原因知道控件的名称,您仍然可以找到已添加到UserControl类型的所有控件。面板的控件集合。像这样:

foreach (Control ctrl in panel1.Controls)
{
    if (ctrl is UserControl)
    {
        // Found a UserControl!
        // (do something here...)
    }
}

记住 Tag<每个控件上提供的 /code> 属性为您提供了一种唯一标识它的方法。如果您不知道名称,也可以检查该属性是否匹配。

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.

枫以 2024-11-05 06:31:48

不确定打开是什么意思,但您可以处理 Panel 类上的 ControlAdded 事件以在添加控件时捕获...

panel1.ControlAdded += new ControlEventHandler(p_ControlAdded);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文