Results 1 to 5 of 5

Thread: Select TreeNode using Fullpath (of node)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2008
    Posts
    72

    Select TreeNode using Fullpath (of node)

    Hello

    I have a treeview that is populated. The user will have some node selected in that treeview. A refresh function is called, and the program remembers the full path, so that after the treeview is cleared and repopulated, the same user selected node can be reselected for them automatically.

    I have the full path of a tree node. If the full path is "nodea\nodeb\nodec" how can I automatically select that node using code?

    Thank you!
    Last edited by Bohlas_DuBhunkus; Mar 11th, 2010 at 05:00 PM. Reason: Added more details.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Select TreeNode using Fullpath (of node)

    In VB6 each Node in Treeview has a key value. With this key you can find node very quickly.

    AFAIK and I could be wrong but since VB.NET treeview control doesn't have this property, it seems you need to make a recursive subroutine to find the node you are searching for...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Select TreeNode using Fullpath (of node)

    See this link if this helps you using 'Tags'

    http://www.andreavb.com/forum/viewtopic_4055.html
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2008
    Posts
    72

    Re: Select TreeNode using Fullpath (of node)

    I was hoping there was some way to select the node directly using the path that I couldn't find.

    Something like
    TreeView1.SelectedNode=TreeView.NodeFromPath("nodea\nodeb\nodec")

    And I already have the tags chuck full of data pertaining to the node, so I cant use those.

    I guess a sub routine would work, but I'm dealing with 10,000+ nodes and the quicker the better.

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Select TreeNode using Fullpath (of node)

    There is no method provided, but you can easily construct one of your own.

    Try this:
    Add this to a module
    vb.net Code:
    1. Imports System.Runtime.CompilerServices
    2.  
    3. Module MyTreeViewExtensions
    4.     <Extension()> _
    5.     Public Function AddTreeNode(ByVal parentNode As TreeNode, ByVal text As String) As TreeNode
    6.         Dim tn As New TreeNode(text)
    7.         tn.Name = parentNode.Name & parentNode.TreeView.PathSeparator & text
    8.         parentNode.Nodes.Add(tn)
    9.         Return tn
    10.     End Function
    11.  
    12.     <Extension()> _
    13.     Public Function AddTreeNode(ByVal treeView As TreeView, ByVal text As String) As TreeNode
    14.         Dim tn As New TreeNode(text)
    15.         tn.Name = text
    16.         treeView.Nodes.Add(tn)
    17.         Return tn
    18.     End Function
    19.  
    20.     <Extension()> _
    21.     Public Function NodeFromPath(ByVal treeView As TreeView, ByVal path As String) As TreeNode
    22.         Dim tn As TreeNode = treeView.Nodes.Find(path, True).First()
    23.         Return tn
    24.     End Function
    25.  
    26. End Module

    Now use these methods to add new nodes and to search for nodes.
    e.g.
    vb.net Code:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.         '' adding tree nodes
    3.         TreeView2.AddTreeNode("nodea")
    4.         TreeView2.Nodes(0).AddTreeNode("nodeb")
    5.         TreeView2.Nodes(0).Nodes(0).AddTreeNode("nodec")
    6.  
    7.  
    8.         '' searching for node by path demo
    9.         TreeView2.SelectedNode = TreeView2.NodeFromPath("nodea\nodeb\nodec")
    10.         TreeView2.ExpandAll()
    11.         TreeView2.Select()
    12.     End Sub

    Of course I haven't put any error handling code for simplicity of understanding. You should put appropriate error handlers. e.g. if no node is found with the path you specified etc.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

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