Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Findcontrol using a non recursive approach

, 25 Nov 2011
Rate this:
Please Sign up or sign in to vote.
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:

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)

Share

About the Author

Kabwla.Phone
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 appliction or a patient.
Oddly enough, more people care about the death of their application, than the massacre op people...

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 25 Nov 2011
Article Copyright 2011 by Kabwla.Phone
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid