【问题标题】:Looping through controls循环通过控件
【发布时间】:2009-05-15 09:32:52
【问题描述】:

在我的代码中,我需要遍历 GroupBox 中的控件,并且仅当它是 ComboBox 时才处理该控件。我正在使用代码:

foreach (System.Windows.Forms.Control grpbxChild in this.gpbx.Controls)
{
    if (grpbxChild.GetType().Name.Trim() == "ComboBox")
    {
        // Process here
    }
}

我的问题是:可以只从 GroupBox 中获取组合框,而不是循环遍历所有控件并仅处理组合框吗?像这样的:

foreach (System.Windows.Forms.Control grpbxChild in this.gpbx.Controls.GetControlsOfType(ComboBox))
{
    // Process here
}

【问题讨论】:

    标签: asp.net controls c#-2.0


    【解决方案1】:

    由于您使用的是 C# 2.0,因此您很不走运。你可以自己写一个函数。在 C# 3.0 中,您只需:

    foreach (var control in groupBox.Controls.OfType<ComboBox>())
    {
        // ...
    }
    

    C# 2.0 解决方案:

    public static IEnumerable<T> GetControlsOfType<T>(ControlCollection controls)
        where T : Control
    {
        foreach(Control c in controls)
            if (c is T)
                yield return (T)c;
    }
    

    你会这样使用:

    foreach (ComboBox c in GetControlsOfType<ComboBox>(groupBox.Controls))
    {
        // ...
    }
    

    【讨论】:

    • +1 用于提及 OfType 语法,但呈现的“伪解决方案”仍然需要循环遍历所有子控件。这并没有解决基本问题。
    • @Cerebrus:如果你不需要对它做任何事情,它就不需要循环。如果您只想要组合框的集合,您可以这样做: List list = new List(GetControlsOfType(groupBox.Controls));
    • 感谢 Mehrdad……这行得通!!!不要称之为伪解决方案。这是解决方案。你只是缺少 where 子句。请编辑响应以添加它: public static IEnumerable GetControlsOfType(Control.ControlCollection controls) where T : Control
    • 哦,是的,你是对的。这是必需的,因为你正在做演员。我没注意到。
    【解决方案2】:

    Mehrdad 非常正确,但是您的语法(即使您使用的是 C# 2.0)过于复杂。

    我觉得这更简单:

    foreach (Control c in gpBx.Controls) 
    { 
      if (c is ComboBox) 
      { 
        // Do something.
      }
    }
    

    【讨论】:

    • 错了。 typeof(c) 总是System.Type 所以typeof(c) is ComboBox 总是假的。你应该这样做c is ComboBox
    • 啊,对了!我的错。修改了我的帖子。
    • 是的。现在是正确的。为了完整起见,如果c 不是类型, typeof(c) 将无法在 C# 中编译。也就是说,你根本不能对变量执行 typeof。
    • 是的,我正要告诉你它不会编译,因为 typeof 关键字计算的是内置 System.Type,而不是运行时类型。谢谢指正!
    • 我正在使用 GetType().Name 因为我有一个开关盒。 c is ComboBox 绝对是比较数据类型的更好方法。谢谢
    【解决方案3】:
    foreach (System.Windows.Forms.Control grpbxChild in this.gpbx.Controls)
    {
        if (grpbxChild is ComboBox)
        {
            // Process here
        }
    }
    

    【讨论】:

      【解决方案4】:
      if (!(grpbxChild is System.Windows.Forms.Combobox)) continue;
      
      // do your processing goes here
      grpbxChild.Text += " is GroupBox child";
      

      【讨论】:

      • 这将编译。 is 的优先级低于!。结果,编译器将首先尝试将! 应用于失败的 GroupBox 变量。欲了解更多信息:stackoverflow.com/questions/811614/…
      • 是的,你说得对,我的错应该是 if (!(grpbxChild is System.Windows.Forms.Combobox))
      【解决方案5】:
      foreach (Control items in this.Controls.OfType<GroupBox>())
      {
          foreach (ComboBox item in items.Controls.OfType<ComboBox>())
          {
              // your processing goes here
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-19
        • 1970-01-01
        • 2013-12-04
        • 2012-08-05
        相关资源
        最近更新 更多