Help convert Control.ControlCollection => List<Control>

Answered Help convert Control.ControlCollection => List<Control>

  • Thursday, March 20, 2008 11:26 AM
     
     
    Code Snippet

                //ArrayList controlList = new ArrayList(controlsContainer.Controls);

                List<Control> controlList = control.Controls as List<Control>;  // Conversion Error

                controlList.Sort(new ControlComparer());


    In this situlation, how can I make a generic List<Control> from the a Control.ControlCollection ?

All Replies

  • Thursday, March 20, 2008 11:33 AM
     
     Answered
    Is this .Net 3.5? Is so then this will work:

    List
    <Control> controlList = new List<Control>
    ();
    controlList.AddRange(control.Controls.OfType<Control>();
    controlList.Sort(new ControlComparer());

    If not then you need to build the list:
    List<Control> controlList = new List<Control>();
    foreach(Control c in control.Controls)
    {
    controlList.Add(c);
    }
    controlList.Sort(new ControlComparer());
  • Thursday, March 20, 2008 11:37 AM
     
     
    No. It is .NET 2.0

    I use

    Code Snippet

                List<Control> controlList = new List<Control>();

                foreach (Control ctrl in controlsContainer.Controls)

                {

                    controlList.Add(ctrl);

                }


    maybe there is an alternative?
  • Thursday, March 20, 2008 11:41 AM
     
     Answered
    Well control.Controls is of type ControllCollection which is IEnumerable, but not IEnumerable<Control>  so you cant just use add range.  The foreach is probably your best option if you want it in a List<Control>

    If you just wanted to use ArrayList (since it takes anything) you could do:

    ArrayList controlList = new ArrayList ();

    controlList.AddRange(
    controlsContainer.Controls);

    I think you are better off wiht the strongly types list and the foreach loop.

  • Tuesday, October 26, 2010 12:42 PM
     
      Has Code
    List<Control> controlList = new List<Control>(control.Controls.Cast<Control>());
    

  • Tuesday, May 15, 2012 5:58 PM
     
     Proposed Has Code
    Well control.Controls is of type ControllCollection which is IEnumerable, but not IEnumerable<Control>  ...

    Given that, isn't the fastest and easiest way to just do:

    controlsContainer.Controls.Cast<Control>().ToList();

    The "Cast<Control>()" takes you from an IEnumerable to an IEnumerable<Control>, which is all that's necessary to open the door to LINQ magic (and by that I mean all your favorite extension methods in System.Linq.Enumerable :)

    It's possibly slower, but it's declarative, which is always a plus.  Also, you can drop the ".ToList()" and do other IEnumerable LINQ operations at that point, such as Where, Select, OfType (OfType is often very useful with a list of Controls), etc etc.


    • Proposed As Answer by PBarranis Tuesday, May 15, 2012 5:58 PM
    • Edited by PBarranis Tuesday, May 15, 2012 5:59 PM HTML issues
    •