Click here to Skip to main content
15,400,433 members
Articles / Desktop Programming / Windows Forms
Alternative
Tip/Trick
Posted 24 Jan 2012

Tagged as

Stats

11.8K views
2 bookmarked

Retrieving TreeView nodes as IEnumerable

Rate me:
Please Sign up or sign in to vote.
3.20/5 (5 votes)
24 Jan 2012CPOL
You can make this slightly more reusable by targeting the IEnumerable interface and providing a function to return the children of each item:public static class EnumerableExtensions{ private static IEnumerable DescendantsAndSelfIterator( IEnumerable source, ...
You can make this slightly more reusable by targeting the IEnumerable<T> interface and providing a function to return the children of each item:

C#
public static class EnumerableExtensions
{
    private static IEnumerable<T> DescendantsAndSelfIterator<T>(
        IEnumerable<T> source, 
        Func<T, IEnumerable<T>> getChildren)
    {
        var queue = new Queue<IEnumerable<T>>();
        queue.Enqueue(source);

        while (0 != queue.Count)
        {
            foreach (var current in queue.Dequeue())
            {
                yield return current;

                var children = getChildren(current);
                if (null != children) queue.Enqueue(children);
            }
        }
    }

    public static IEnumerable<T> DescendantsAndSelf<T>(
        this IEnumerable<T> source, 
        Func<T, IEnumerable<T>> getChildren)
    {
        if (null == source) throw new ArgumentNullException("source");
        if (null == getChildren) return source;
        return DescendantsAndSelfIterator(source, getChildren);
    }
}


Your TreeView extension method then becomes:
C#
public static class TreeViewExtension
{
    public static IEnumerable<TreeNode> AllTreeNodes(this TreeView treeView)
    {
        if (null == treeView) throw new ArgumentNullException("treeView");

        return treeView.Nodes.Cast<TreeNode>()
            .DescendantsAndSelf(n => n.Nodes.Cast<TreeNode>());
    }
}

License

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

Share

About the Author

Richard Deeming
Software Developer CodeProject
United Kingdom United Kingdom
I started writing code when I was 8, with my trusty ZX Spectrum and a subscription to "Input" magazine. Spent many a happy hour in the school's computer labs with the BBC Micros and our two DOS PCs.

After a brief detour into the world of Maths, I found my way back into programming during my degree via free copies of Delphi and Visual C++ given away with computing magazines.

I went straight from my degree into my first programming job, at Trinet Ltd. Eleven years later, the company merged to become ArcomIT. Three years after that, our project manager left to set up Nevalee Business Solutions, and took me with him. Since then, we've taken on four more members of staff, and more work than you can shake a stick at. Smile | :)

Between writing custom code to integrate with Visma Business, developing web portals to streamline operations for a large multi-national customer, and maintaining RedAtlas, our general aviation airport management system, there's certainly never a dull day in the office!

Outside of work, I enjoy real ale and decent books, and when I get the chance I "tinkle the ivories" on my Technics organ.

Comments and Discussions

 
GeneralReason for my vote of 5 Your alternative is truly more reusa... Pin
intrueder24-Jan-12 21:27
Memberintrueder24-Jan-12 21:27 

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.