Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 450
  • Last Modified:

Treeview Nodes drill down

I have a treeview with a directory structure...

each node is a sub directory...

when clicking on a node, the path value is created based upon parent and current node text...

I can build a path string with parent and node, but clicking levels deeper than that I am having trouble creating the complete path...

How can I create a navigation path from 4 or 5 node levels?
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Parent != null)
            {
                dirParent = e.Node.Parent.Text.ToString();
                dirNav = dirParent + @"\" + e.Node.Text.ToString();
            }
            else
            {
                dirNav = e.Node.Text.ToString();
            }
            MessageBox.Show(dirNav);
        }

Open in new window

0
conrad2010
Asked:
conrad2010
2 Solutions
 
Gautham JanardhanCommented:

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            StringBuilder builder = new StringBuilder();
            BuildNavString(e.Node, builder);
            MessageBox.Show(builder.ToString());
        }
        void BuildNavString(TreeNode Node, StringBuilder builder)
        {
 
            if (Node.Parent != null)
            {
                builder.Insert(0, @"\" + Node.Text);
                BuildNavString(Node.Parent, builder);
            }
            else
            {
                builder.Insert(0, Node.Text);
            }
        }

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
Or, without the expensive recursive call:
        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            TreeNode node = e.Node;
 
            while (node != null)
            {
                sb.Insert(0, @"\" + node.Text);
                node = node.Parent;
            }
 
            string dirNav = sb.ToString().Remove(0, 1);
            MessageBox.Show(dirNav);
        }

Open in new window

0
 
conrad2010Author Commented:
both solutions work great!
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now