【问题标题】:How do I join two control collections for use with foreach statement如何加入两个控件集合以与 foreach 语句一起使用
【发布时间】:2012-08-10 07:25:05
【问题描述】:

根据问题,我的表单上有两个面板,一个名为 leftPanel,一个名为 rightPanel。这些控制我表单上的两列布局。

我在这些面板中也有折叠/展开的组合框,我希望遍历每个组合框以刷新布局,使它们在大小改变时相互抢购。

这是我的代码:

    private void RefreshLayout()
    {
        int rollingTopLeft = grpiAddressDetails.Top + grpiAddressDetails.Height + 10;
        int rollingTopRight = grpiBranding.Top + grpiBranding.Height + 10;
        foreach(Control temp in leftPanel.Controls && rightPanel.Controls)
        {
            if (temp is GroupBox)
            {
                if (!(temp.Name.Contains("grpi")))    // Top group boxes have 'i' as the 4th character in their name.
                {
                    if (temp.Parent == leftPanel)
                    {
                        temp.Top = rollingTopLeft;
                        rollingTopLeft += temp.Height + 10;
                    }
                    else if(temp.Parent == rightPanel)
                    {
                        temp.Top = rollingTopRight;
                        rollingTopRight += temp.Height + 10;
                    }
                }
            }
        }
    }

这是我需要加入集合的行:

foreach(Control temp in leftPanel.Controls && rightPanel.Controls)

我意识到 && 不起作用,也尝试过 Controls.Concat 但 Control Collections 似乎没有这个功能。 希望一切都清楚!

【问题讨论】:

    标签: c# collections controlcollection


    【解决方案1】:
    var allControls = leftPanel.Controls.Cast<Control>().Concat(rightPanel.Controls.Cast<Control>());
    foreach(Control temp in allControls)
    {
         //...
    }
    

    【讨论】:

    • 使用.Cast&lt;Control&gt;需要什么?
    猜你喜欢
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多