Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Help with setting and expanding nodes in treeview

Help with setting and expanding nodes in treeview

Scheduled Pinned Locked Moved C#
help
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    flow5555
    wrote on last edited by
    #1

    Hey everyone, I have a problem with seting and expanding nodes in a explorer clone program I am trying to make. The problems comes as when I am trying to open a folder in the right side of the explore bar (the right side is a TreeView and the left is a ListView). I can't find a method that will allow me to change the selected node in a treeView and also expand it. Please help. Part of my program's source code is here private void lv_ItemActivate(object sender, System.EventArgs e) { ListView lv = (ListView)sender; foreach (ListViewItem lvi in lv.SelectedItems) { string fullPath = tvw.SelectedNode.FullPath.ToString() + "\\" +lvi.Text.ToString(); MessageBox.Show(lvi.Text); DirectoryInfo di = new DirectoryInfo(fullPath); if ((di.Attributes & FileAttributes.Directory)!=0) { //here is what I have being attempting but to no avil because the selected node property is read only tvw.SelectedNode = new TreeNode(lvi.Text); tvw.SelectedNode.FullPath = fullPath; tvw.SelectedNode.Expand(); return; flow PS: Thanks for the help in advance, I am new to the forum and I hope I can contribute and learn a lot in here.

    H 1 Reply Last reply
    0
    • F flow5555

      Hey everyone, I have a problem with seting and expanding nodes in a explorer clone program I am trying to make. The problems comes as when I am trying to open a folder in the right side of the explore bar (the right side is a TreeView and the left is a ListView). I can't find a method that will allow me to change the selected node in a treeView and also expand it. Please help. Part of my program's source code is here private void lv_ItemActivate(object sender, System.EventArgs e) { ListView lv = (ListView)sender; foreach (ListViewItem lvi in lv.SelectedItems) { string fullPath = tvw.SelectedNode.FullPath.ToString() + "\\" +lvi.Text.ToString(); MessageBox.Show(lvi.Text); DirectoryInfo di = new DirectoryInfo(fullPath); if ((di.Attributes & FileAttributes.Directory)!=0) { //here is what I have being attempting but to no avil because the selected node property is read only tvw.SelectedNode = new TreeNode(lvi.Text); tvw.SelectedNode.FullPath = fullPath; tvw.SelectedNode.Expand(); return; flow PS: Thanks for the help in advance, I am new to the forum and I hope I can contribute and learn a lot in here.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      The TreeView.SelectedNode property is NOT read-only, but TreeNode.FullPath is (which you're trying to set). All you need to do is recursively find your TreeNode (based on the path if you like) and then set TreeView.SelectedNode to it. If you want to expand it, call the Expand method on your TreeNode.

      Microsoft MVP, Visual C# My Articles

      F 1 Reply Last reply
      0
      • H Heath Stewart

        The TreeView.SelectedNode property is NOT read-only, but TreeNode.FullPath is (which you're trying to set). All you need to do is recursively find your TreeNode (based on the path if you like) and then set TreeView.SelectedNode to it. If you want to expand it, call the Expand method on your TreeNode.

        Microsoft MVP, Visual C# My Articles

        F Offline
        F Offline
        flow5555
        wrote on last edited by
        #3

        Hey thanks for the help, I have being thinking of using the Parent property to recurse through the tree node, but I what I don't understand is that after this I am still going to find a full path, and I wondering what to do with that. Thanks for the help.

        H 1 Reply Last reply
        0
        • F flow5555

          Hey thanks for the help, I have being thinking of using the Parent property to recurse through the tree node, but I what I don't understand is that after this I am still going to find a full path, and I wondering what to do with that. Thanks for the help.

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          You don't recurse up using the Parent, you recurse through the children using the Nodes property. All children have only one parent, so there's not even a need to use recursion since a simple iterative loop will work just fine. To find a full path, just split the string on the separate character ("\" by default) and look for the TreeNode with the given text like so:

          public TreeNode FindPath(string path)
          {
          if (path == null) throw new ArgumentNullException("path");
          string[] names = path.Split(PathSeparator);
          return FindPathHelper(Nodes, names, 0);
          }
          private TreeNode FindPathHelper(TreeNodeCollection nodes,
          string[] names, int iteration)
          {
          if (nodes != null && names != null &&
          iteration >= 0 && iteration < names.Length)
          {
          foreach (TreeNode node in nodes)
          {
          if (string.Compare(node.Text, names[iteration], true) == 0)
          return node;
          TreeNode child = FindPathHelper(node.Nodes, names, ++iteration);
          if (child != null) return child;
          }
          }
          return null;
          }

          Microsoft MVP, Visual C# My Articles

          K 1 Reply Last reply
          0
          • H Heath Stewart

            You don't recurse up using the Parent, you recurse through the children using the Nodes property. All children have only one parent, so there's not even a need to use recursion since a simple iterative loop will work just fine. To find a full path, just split the string on the separate character ("\" by default) and look for the TreeNode with the given text like so:

            public TreeNode FindPath(string path)
            {
            if (path == null) throw new ArgumentNullException("path");
            string[] names = path.Split(PathSeparator);
            return FindPathHelper(Nodes, names, 0);
            }
            private TreeNode FindPathHelper(TreeNodeCollection nodes,
            string[] names, int iteration)
            {
            if (nodes != null && names != null &&
            iteration >= 0 && iteration < names.Length)
            {
            foreach (TreeNode node in nodes)
            {
            if (string.Compare(node.Text, names[iteration], true) == 0)
            return node;
            TreeNode child = FindPathHelper(node.Nodes, names, ++iteration);
            if (child != null) return child;
            }
            }
            return null;
            }

            Microsoft MVP, Visual C# My Articles

            K Offline
            K Offline
            korell
            wrote on last edited by
            #5

            Hello Heath, I know this thread over 5 years old and I hope you're still subscribing to it. I'm also trying to expand the path to a specific node in a treeview (i.e. a node that has just been added by a user). I almost understand your code example. However my question is, where did you get the TreeNodeCollection for the "Nodes" parameter in the initial FindPathHelper call (within the FindPath function)? I don't see it declared anywhere. Thanks, Sacha

            H 1 Reply Last reply
            0
            • K korell

              Hello Heath, I know this thread over 5 years old and I hope you're still subscribing to it. I'm also trying to expand the path to a specific node in a treeview (i.e. a node that has just been added by a user). I almost understand your code example. However my question is, where did you get the TreeNodeCollection for the "Nodes" parameter in the initial FindPathHelper call (within the FindPath function)? I don't see it declared anywhere. Thanks, Sacha

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              As mentioned, "you recurse through the children using the Nodes property". The Nodes property is on TreeView class and on the TreeNode class (which is used to recurse into subnodes).

              This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups