CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2008
    Posts
    13

    c# treeView help?

    I'm using an arraylist to store an array like...
    files.Add(new StoredFile(nodeTitle, nodeName, parentNode));
    I'm trying to add each element of the arrayList to the treeview using a loop.

    foreach (StoredFile f in files)
    {
    if (f.gsParentNode.Equals(mainNode.Name))
    child = addTreeNode(child, mainNode, f);
    MessageBox.Show(child.Name.ToString())
    ...
    private TreeNode addTreeNode(TreeNode newNode, TreeNode rootNode, StoredFile f)
    {
    newNode = new TreeNode();
    newNode.Name = f.gsFileName;
    newNode.Text = f.gsTitle;
    rootNode.Nodes.Add(newNode);
    return newNode;
    }

    If I'm adding child nodes to mainNode from the array list. Is there a way of looping through all child nodes to check whether any of them are a parent of another node, which is to be added?

  2. #2
    Join Date
    May 2008
    Posts
    7

    Re: c# treeView help?

    Here is an example of a Windows Form that does what you're asking. This is VS 2005, hence the partial class.

    I created a StoredFile class just so the example can run on its own and you can plug parts of this example directly into your own code.

    You'll need to add a TreeView named treeView1. (Default names are used here for simplicity.)

    Anonymous methods rule.

    PHP Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace 
    WindowsApplication1
    {
        public 
    partial class Form1 Form
        
    {
            public 
    Form1()
            {
                
    InitializeComponent();
            }

            private 
    void Form1_Load(object senderEventArgs e)
            {
                List<
    StoredFilefiles = new List<StoredFile>();
                
    files.AddRange(new StoredFile[]
                    {
                        new 
    StoredFile("fruits""fruits.txt"""),
                        new 
    StoredFile("red fruits""redfruits.txt""fruits.txt"),                 
                        new 
    StoredFile("cherry""cherry.txt""redfruits.txt"),
                        new 
    StoredFile("raspberry""raspberry.txt""redfruits.txt"),
                        new 
    StoredFile("apple""apple.txt""redfruits.txt"),                   
                        new 
    StoredFile("yellow fruits""yellowfruits.txt""fruits.txt"),
                        new 
    StoredFile("lemon""lemon.txt""yellowfruits.txt"),
                        new 
    StoredFile("banana""banana.txt""yellowfruits.txt"),
                        new 
    StoredFile("vegetables""vegetables.txt"""),
                        new 
    StoredFile("green vegetables""greenvegetables.txt""vegetables.txt"),                 
                        new 
    StoredFile("spinach""spinach.txt""greenvegetables.txt"),
                        new 
    StoredFile("broccoli""broccoli.txt""greenvegetables.txt"),
                        new 
    StoredFile("zucchini""zucchini.txt""greenvegetables.txt")
                    });

                foreach (
    StoredFile f in files.FindAll(delegate(StoredFile sf) { return sf.gsParentNode == ""; }))
                {
                    
    treeView1.Nodes.Add(f.gsFileNamef.gsTitle);
                }

                foreach (
    TreeNode n in treeView1.Nodes)
                {
                    
    AddNodesRecursive(filesn);
                }
            }

            private 
    void AddNodesRecursive(List<StoredFilefilesTreeNode n)
            {
                foreach (
    StoredFile child in files.FindAll(delegate(StoredFile sf) { return sf.gsParentNode == n.Name; }))
                {
                    
    AddNodesRecursive(filesn.Nodes.Add(child.gsFileNamechild.gsTitle));
                }
            }
        }

        class 
    StoredFile
        
    {
            public 
    string gsTitle;
            public 
    string gsFileName;
            public 
    string gsParentNode;

            public 
    StoredFile(string titlestring fileNamestring parentNode)
            {
                
    gsTitle title;
                
    gsFileName fileName;
                
    gsParentNode parentNode;
            }
        }

    Last edited by theycallmeloopy; May 12th, 2008 at 07:53 PM.
    Please vote for my Google Maps programming contest entry! (#3: IHOPs)
    No registration necessary; just click "Vote." Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured