Link to home
Start Free TrialLog in
Avatar of rizel
rizelFlag for Japan

asked on

populate treeview

I am writing a simple note taking program and somehow I got stuck with this problem.
How do I populate a treeview with this kind of data?  I have little experience with treeviews...

For example:

string[] data = new string[] {"tv","tv|comedy","tv|horror","game","game|pc","game|pc|genre"};

what I want my treeview to look like this:

tv
  |_comedy
  |_horror
game
  |_pc
      |_genre

I thought a recursive method would be useful...but, so far I haven't succeed.

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can actually reduce your node description string to:

    String[] data = new string[] {"tv|comedy", "tv|horror", "game|pc|genre"};
Avatar of rizel

ASKER

Excellent.

It's lot different from my codes which I had struggled to make it work.

Thank you very much. :)
Avatar of rizel

ASKER

found a minor problem...

I  have some duplicate words in node description string, the treeview won't look right due to hashtable that doesn't accept any duplicate words...

example:

test|test|test
abc|test|abc
orange|apple|apple
etc.

any solutions to this problem?
thanks
In that case you would need to traverse the TreeView using a Recursive algorithm....I'll post another solution for ya.  =)
Try this out...

        private void button1_Click(object sender, EventArgs e)
        {
            String[] data = new string[] {"tv|comedy", "tv|horror", "game|pc|genre", "test|test|test", "abc|test|abc", "orange|apple|apple"};            

            treeView1.BeginUpdate();
            treeView1.Nodes.Clear();            
            foreach (String path in data)
            {
                buildNodes(path.Split("|".ToCharArray()), 0, null);
            }
            treeView1.ExpandAll();
            treeView1.EndUpdate();
        }

        private void buildNodes(String[] path, int level, System.Windows.Forms.TreeNode parentNode)
        {
            System.Windows.Forms.TreeNode node = null;

            if (parentNode == null)
            {              
                foreach (System.Windows.Forms.TreeNode curNode in treeView1.Nodes)
                {
                    if (curNode.Text.Equals(path[level]))
                    {
                        node = curNode;
                        break;
                    }
                }
            }
            else
            {
                foreach (System.Windows.Forms.TreeNode curNode in parentNode.Nodes)
                {
                    if (curNode.Text.Equals(path[level]))
                    {
                        node = curNode;
                        break;
                    }
                }
            }

            if (node == null)
            {
                node = new System.Windows.Forms.TreeNode(path[level]);
                if (parentNode == null)
                {
                    treeView1.Nodes.Add(node);
                }
                else
                {
                    parentNode.Nodes.Add(node);
                }
            }

            if (level < path.GetUpperBound(0))
            {
                buildNodes(path, ++level, node);
            }
        }
Did this fix your problem rizel?
Avatar of rizel

ASKER

Sorry, I was away for few days...I finally got a chance to write your code today.
Yes, it worked fine perfectly even with multi-language texts. :)

Thanks a bunch :)
Cool...   =)