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. How to get the last node of a TreeView

How to get the last node of a TreeView

Scheduled Pinned Locked Moved C#
questiontutorial
5 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.
  • C Offline
    C Offline
    Chals
    wrote on last edited by
    #1

    Hello, How can I get the last node in the hierarchy of a treeview? Thanks

    X R 2 Replies Last reply
    0
    • C Chals

      Hello, How can I get the last node in the hierarchy of a treeview? Thanks

      X Offline
      X Offline
      xilefxilef
      wrote on last edited by
      #2

      If you mean the last child, then: someParentTreeNode.LastNode or int c = treeView1.Nodes.Count treeView1.Nodes[c-1];

      C 1 Reply Last reply
      0
      • X xilefxilef

        If you mean the last child, then: someParentTreeNode.LastNode or int c = treeView1.Nodes.Count treeView1.Nodes[c-1];

        C Offline
        C Offline
        Chals
        wrote on last edited by
        #3

        Thx for replying but that wont work i = Tree.GetNodeCount(true) will return the number of nodes in the tree including the sub trees, but Nodes[i] will throw an exception as Node.Nodes contains only the nodes in the node collection of the node Im searching (yeah its confusing). Say the tree has 500 nodes but only 4 at first level, doing this: n = Tree.GetNodeCount(true); TreeNode last = Tree.Nodes[n]; // exception because Tree.Nodes has 4 elements and Im trying to get the 500th. what I need is the LAST node in the last level of the tree.

        X 1 Reply Last reply
        0
        • C Chals

          Thx for replying but that wont work i = Tree.GetNodeCount(true) will return the number of nodes in the tree including the sub trees, but Nodes[i] will throw an exception as Node.Nodes contains only the nodes in the node collection of the node Im searching (yeah its confusing). Say the tree has 500 nodes but only 4 at first level, doing this: n = Tree.GetNodeCount(true); TreeNode last = Tree.Nodes[n]; // exception because Tree.Nodes has 4 elements and Im trying to get the 500th. what I need is the LAST node in the last level of the tree.

          X Offline
          X Offline
          xilefxilef
          wrote on last edited by
          #4

          Well, then you may need to use recursion. You can try something like this to get to the deepest node of the tree: private void DepthSearch(TreeNodeCollection nodes, int depth) { int newDepth = depth; for(int i = 0; i < nodes.Count; i++) { if(nodes[i].NextNode == null) { //your code here; } newDepth++; this.TreeSearch(nodes[i].Nodes, newDepth); } } so, calling DepthSearch(TreeView.Nodes, 0) and inserting code (where it says 'your code') that populates some data structure with the current node and the depth, should give you the ability to compare the depths and choose the deepest node.

          1 Reply Last reply
          0
          • C Chals

            Hello, How can I get the last node in the hierarchy of a treeview? Thanks

            R Offline
            R Offline
            Rcms Support
            wrote on last edited by
            #5

            TreeView1.Nodes[TreeView1.Nodes.Count - 1];

            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