如何检索父控件及其子控件 - Winforms C# 2

发布于 2024-10-03 07:52:05 字数 783 浏览 4 评论 0原文

我想检索表单的控件。要做到这一点:

internal static IEnumerable<Control> EnumereTousControle(Control controleParent)
{
    foreach (Control subControl in controleParent.Controls)
    {
        yield return subControl;

        foreach (Control c in EnumereTousControle(subControl))
            yield return c;
    }
}

它工作正常,父控件的所有子控件都被检索。但我也需要这个方法返回父控件。我尝试了这个,但它不起作用(因为该方法是递归的):

internal static IEnumerable<Control> EnumereAllControls(Control parentControl)
{
    yield return parentControl; // does not work

    foreach (Control subControl in parentControl.Controls)
    {
        yield return subControl;

        foreach (Control c in EnumereAllControls(subControl))
            yield return c;
    }
}

感谢您的帮助!

I want to retrieve controls of a form. To do this :

internal static IEnumerable<Control> EnumereTousControle(Control controleParent)
{
    foreach (Control subControl in controleParent.Controls)
    {
        yield return subControl;

        foreach (Control c in EnumereTousControle(subControl))
            yield return c;
    }
}

It works fine, all children of the parent control are retrieved. But I need this method returns the parent control too. I tried this but it don't works (because the method is recursive) :

internal static IEnumerable<Control> EnumereAllControls(Control parentControl)
{
    yield return parentControl; // does not work

    foreach (Control subControl in parentControl.Controls)
    {
        yield return subControl;

        foreach (Control c in EnumereAllControls(subControl))
            yield return c;
    }
}

Thanks for your help !

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

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

发布评论

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

评论(3

椒妓 2024-10-10 07:52:05

想想你的一行太多了,试试这个:

internal static IEnumerable<Control> EnumereAllControls(Control parentControl)
{
    yield return parentControl;

    foreach (Control subControl in parentControl.Controls)
    {
        foreach (Control c in EnumereAllControls(subControl))
            yield return c;
    }
}

Thnk you just had one line too many, try this:

internal static IEnumerable<Control> EnumereAllControls(Control parentControl)
{
    yield return parentControl;

    foreach (Control subControl in parentControl.Controls)
    {
        foreach (Control c in EnumereAllControls(subControl))
            yield return c;
    }
}
笨笨の傻瓜 2024-10-10 07:52:05
    internal static IEnumerable<Control> EnumereTousControle(Control controleParent) {
        yield return controleParent;
        foreach (Control ctl in controleParent.Controls) {
            foreach (Control child in EnumereTousControle(ctl)) 
                yield return child;
        }
    }

好吧,同样的事情减去你得到的额外收益。

    internal static IEnumerable<Control> EnumereTousControle(Control controleParent) {
        yield return controleParent;
        foreach (Control ctl in controleParent.Controls) {
            foreach (Control child in EnumereTousControle(ctl)) 
                yield return child;
        }
    }

Well, same thing minus the extra yield you got.

不乱于心 2024-10-10 07:52:05

如果这不是约束,您可以将父控件添加到方法外部的结果中。

You could add the parent control to the result from outside the method if that is not a constraint.

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