是否可以将所有这些组合成一个foreach?

时间:2017-11-06 11:37:42

标签: c# foreach controls parallel-foreach

到目前为止,我已经尝试过var而无法让它发挥作用。 代码:

var a = new { drives1, ipAdd1, spotify1, timeAndDate1, activeWindow1, reminders11, weather1 };

然后foreach(控制/ var c in a)并得到错误,我确定必须有一个简单的方法,不需要下面的所有代码。

当前代码:

        foreach (Control c in drives1.Controls)
        {
            if (c.Name.StartsWith("SC")) { c.ForeColor = Color.FromArgb(rr, gg, bb); }
        }
        foreach (Control c in ipAdd1.Controls)
        {
            if (c.Name.StartsWith("SC")) { c.ForeColor = Color.FromArgb(rr, gg, bb); }
        }
        foreach (Control c in spotify1.Controls)
        {
            if (c.Name.StartsWith("SC")) { c.ForeColor = Color.FromArgb(rr, gg, bb); }
        }
        foreach (Control c in timeAndDate1.Controls)
        {
            if (c.Name.StartsWith("SC")) { c.ForeColor = Color.FromArgb(rr, gg, bb); }
        }
        foreach (Control c in activeWindow1.Controls)
        {
            if (c.Name.StartsWith("SC")) { c.ForeColor = Color.FromArgb(rr, gg, bb); }
        }
        foreach (Control c in reminders11.Controls)
        {
            if (c.Name.StartsWith("SC")) { c.ForeColor = Color.FromArgb(rr, gg, bb); }
        }
        foreach (Control c in weather1.Controls)
        {
            if (c.Name.StartsWith("SC")) { c.ForeColor = Color.FromArgb(rr, gg, bb); }
        }

6 个答案:

答案 0 :(得分:2)

至少你可以让它与2个foreachs一起工作:

var a = new List<ContainerControl> { drives1, ipAdd1, spotify1, timeAndDate1, activeWindow1, reminders11, weather1 };
foreach (var container in a) {
    foreach (Control c in container.Controls)
    {
        if (c.Name.StartsWith("SC")) { c.ForeColor = Color.FromArgb(rr, gg, bb); }
    }
}

答案 1 :(得分:1)

如果a中的所有项都有一个定义Controls的公共超类,那么你可以聚合所有控件,过滤你需要的那个,然后迭代它:

var allControls = a.SelectMany(i=>i.Controls.Where(c=>c.Name.StartsWith("SC"));

foreach (var control in allControls)
{
    c.ForeColor = Color.FromArgb(rr, gg, bb);
}

如果没有,那么您可以使用union

var allControls = drives1.Controls
    .Union(ipAdd1.Controls)
    .Union(spotify1.Controls)
    .Union ...
    .Where(c=>c.Name.StartsWith("SC");

答案 2 :(得分:1)

  

他们是所有UserControl,

然后创建一个List<UserControl>作为集合。这样您就可以指定公共数据类型,编译器也不会抱怨。它将循环通过它:

List<UserControl> a = new List<UserControl>{ drives1, ipAdd1, spotify1, timeAndDate1, activeWindow1, reminders11, weather1 };

foreach (UserControl uc in a)
{
    foreach (Control c in uc.Controls)
    {
        if (c.Name.StartsWith("SC")) { c.ForeColor = Color.FromArgb(rr, gg, bb); }
    }
}

说明:

如果查看documentation

中类UserControl的继承层次结构

enter image description here

你会发现它上面的2个级别继承自Control类,它具有你在循环中使用的属性Controls。因此UserControl是您可以用于收藏的最低共同祖先。

答案 3 :(得分:0)

添加方法:

private void SetColor( List<Control> control)
{
    foreach (Control c in control)
    {
       if (c.Name.StartsWith("SC")) { c.ForeColor = Color.FromArgb(rr, gg, bb); }
     }
}

迭代:

foreach (Control c in a)
{
   SetColor(c);
}

答案 4 :(得分:0)

你可以这样做

private static void Edit()
{

   setForeColor(drives1.Controls);
   setForeColor(ipAdd1.Controls);
   setForeColor(spotify1.Controls);
   setForeColor(timeAndDate1.Controls);
   setForeColor(activeWindow1.Controls);
   setForeColor(reminders11.Controls);
   setForeColor(weather1.Controls);

}


private static void setForeColor(Controls controls)
{
        foreach (Control c in controls)
        {
            if (c.Name.StartsWith("SC"))
            { 
                c.ForeColor = Color.FromArgb(rr, gg, bb); 
            }
        }

}

答案 5 :(得分:0)

您可以将它们加入到单个IEnumerable中,然后使用where条件来获取匹配器:

namespace ConsoleApp1 {

    public class Control {
        public Control(string name) {
            this.Name = name;
        }

        public string Name { get; set; }
    }

    class Program {
        static void Main(string[] args) {
            var list1 = new List<Control> { new Control("a"), new Control("b"), new Control("c") };
            var list2 = new List<Control> { new Control("a"), new Control("b"), new Control("c") };
            var list3 = new List<Control> { new Control("a"), new Control("b"), new Control("c") };

            foreach (var ctrl in GetList(list1, list2, list3).Where(c => c.Name.StartsWith("a"))) {
                ctrl.ForeColor = Color.FromArgb(rr, gg, bb);
            }
        }

        private static IEnumerable<Control> GetList (params IEnumerable<Control>[] controls) {
            return controls.SelectMany(x => x);
        }
    }
}