Click here to Skip to main content
15,867,308 members
Articles / Ienumerable
Alternative
Tip/Trick

Findcontrol using a non recursive approach

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
25 Nov 2011CPOL 13.6K   7
Ha! I get it....I am actually using an adaptation of this technique in production code.But the adapted code required a dept-first search and this original pattern is width-first.Which brings us these new and improved versions:public static List FindControlsWidthFirst( Control...

Ha! I get it....



I am actually using an adaptation of this technique in production code.
But the adapted code required a dept-first search and this original pattern is width-first.
Which brings us these new and improved versions:


C#
public static List<control> FindControlsWidthFirst( Control controlTop )
{
    List<control> list = new List<control>();
    list.Add(controlTop); 
    //NOTE: we are not using a foreach, 
    //      which means that as long as the current parent has children,
    //      list.Count increases... which means we will automatically 
    //      Get a next itteration because 'i' is still < than the count.
    for (int i = 0; i < list.Count; ++i)
    {
        list.AddRange(list[i].Controls);
    } 
    return list;
}
public static List<control> FindControlsDepthFirst( Control controlTop )
{
    List<control> list = new List<control>();
    list.Add(controlTop); 
    //NOTE: we are not using a foreach, 
    //      which means that as long as the current parent has children,
    //      list.Count increases... which means we will automatically 
    //      Get a next itteration because 'i' is still < than the count.
    for (int i = 0; i < list.Count; ++i)
    {
        // insert into the position for the next itteration:
        list.InsertRange(i + 1, list[i].Controls);
    } 
    return list;
}
</control></control></control></control>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Netherlands Netherlands
Doing that 'computer thing' ever since the C64.

Sometimes I feel that being a programmer is much like being a doctor: You just have to know everything and if you don't, something dies.

Either being an application or a patient.

Oddly enough, more people care about the death of their application, than the massacre of people...

Comments and Discussions

 
Generalyes, i have this namespace but everytime i get this error me... Pin
Member 38257113-Feb-12 2:05
Member 38257113-Feb-12 2:05 
GeneralRe: I would not know, I have only used it in winform environment... Pin
Kabwla.Phone13-Feb-12 2:26
Kabwla.Phone13-Feb-12 2:26 
Generalcannot convert from 'System.Web.UI.ControlCollection' to 'Sy... Pin
Member 38257112-Feb-12 23:33
Member 38257112-Feb-12 23:33 
GeneralRe: Assembly mscorlib.dll, v4.0.30319 namespace System.Collecti... Pin
Kabwla.Phone12-Feb-12 23:56
Kabwla.Phone12-Feb-12 23:56 
GeneralReason for my vote of 5 Perfect if you don't care whether yo... Pin
PopeDarren21-Dec-11 4:49
PopeDarren21-Dec-11 4:49 
GeneralVery clever. However, without the comment explaining that th... Pin
tpwright44238-Nov-11 1:52
tpwright44238-Nov-11 1:52 
GeneralRe: Why is that. Explain please. Pin
rj458-Nov-11 8:22
rj458-Nov-11 8:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.