tags:

views:

229

answers:

1
+5  A: 

You can use a queue to get rid of recursion if you want.

        public static IEnumerable<T> FindChildControls<T>(Control parentControl,
        Predicate<Control> predicate) where T : Control
        {
            Queue<Control> q = new Queue<Control>();

            foreach (Control item in parentControl.Controls)
            {
                q.Enqueue(item);
            }

            while (q.Count > 0)
            {
                Control item = q.Dequeue();
                if (predicate(item))
                    yield return (T)item;

                foreach (Control child in item.Controls)
                {
                    q.Enqueue(child);
                }
            }

        }
Khalil Dahab
This is actually very neat, In my situation (heavy page control tree and used about 4-5 time each render) do you think this will be optimal?
James Couvares