Selecting form controls that are either inside or outside a rectangle I have a function which loops over the controls on a form and checks to see if it's in a given rect, and then adds it or doesn't add it to a list&lt;&gt;.  The parameter bContained is used to determine if we want a list of "in the rect" or "not in the rect" controls.

List&lt;Control&gt; getControls(Control container, Rectangle rect, bool bContained = false)
{
    List&lt;Control&gt; Selected = new List&lt;Control&gt;();

    rect = RectangleToClient(rect);

    foreach (Control ctl in container.Controls)
    {
        //Do we want selected or non-selected controls
        if (bContained)
        {
            if (rect.IntersectsWith(ctl.Bounds)) Selected.Add(ctl);
            foreach (Control ct in ctl.Controls) Selected.Add(ct);
        }
        else
        {
            if (!rect.IntersectsWith(ctl.Bounds)) Selected.Add(ctl);
            foreach (Control ct in ctl.Controls) Selected.Add(ct);
        }
    }

    return Selected;
}
You can make things more concise with LINQ.  Here's one way that should work:

List&lt;Control&gt; getControls(Control container, Rectangle rect, bool bContained = false)
{
    rect = RectangleToClient(rect);
    return (from Control ctl in container.Controls
                  where rect.IntersectsWith(ctl.Bounds) == bContained                     
                  select new List&lt;Control&gt; { ctl }.Union(ctl.Controls.OfType&lt;Control&gt;()))
                  .SelectMany(x =&gt; x).ToList();
}


This basically does the same thing, filters the collection according to bContained.  Creates a collection of  lists that each contain the parent control and its sub-controls.  Then flattens them into one list.