2 Replies - 3337 Views - Last Post: 16 February 2013 - 01:56 PM Rate Topic: -----

#1 lacrue79  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 12-August 09

How to Access Treeview Child Nodes

Posted 15 February 2013 - 06:09 PM

Hello, I am using the treeview control for the first time. My tree view is populated with parent and child nodes already.

I'm trying to locate a specific child node so that I can update the text. I have attached a screen print of my treeview.

When I execute the following code only the 4 parent nodes of "Main", "Request Info", "Business Only", and "PHI Data" are detected(see attached screen print). I can't seem to figure out how to get to the children of the parent nodes.

 
For intX = 0 To FrmCreateCQSummary.tvSummary.Nodes.Count - 1
    If FrmCreateCQSummary.tvSummary.Nodes(intX).Name = "NodeAssignedTo" Then
       FrmCreateCQSummary.tvSummary.Nodes(intX).Text = FrmCreateCQSummary.tvSummary.Nodes(intX).Text & " " & assignedToStr
     End If
Next 



How can I get to the childnode of "NodeAssignedTo" to update the text via code? I will also need to access the children of the Child node titled "Description" (see screen print). Any pointers will be greatly appreciated. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: How to Access Treeview Child Nodes

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: How to Access Treeview Child Nodes

Posted 15 February 2013 - 07:36 PM

It is a lesson in recursion. I would make a function that returned the desired node based on the name of the parent node and then the name of the child node your looking for then return that and you now can do whatever you need.

Psuedo code:
Private Function getChildNodeOfParent(child As String, parent As String, basenode As TreeNode) As TreeNode
  Dim result As TreeNode = Nothing
  Dim found As Boolean
  For Each treenode in basenode.Nodes
     if treenode.Name = parent then
        search for child of parent - call function again in here
        if found set boolean, set result, exit inner for loop
     if found exit outer for loop
  Next
  Return result
End Function


Usage:
Dim tn As TreeNode = getChildNodeOfParent(child, parent, me.TreeNode1.Nodes(0))
If tn IsNot Nothing Then tn.Text = "I Found You!"

Was This Post Helpful? 1
  • +
  • -

#3 lacrue79  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 12-August 09

Re: How to Access Treeview Child Nodes

Posted 16 February 2013 - 01:56 PM

At Hawk, thank you so much for the feedback. It was very helpful. Here is what I did and it works beautifully for what I needed to accomplish.

Private Sub clearSummaryValues(ByVal basenode As TreeNode)
        'will loop through the children of the identified parent nodes 
        'my treeview has prepopulsted text for each node which are
        'fiel headings. I needed to retain the hard coded headings but
        'clear the text after the heading. 
        Dim wkStr, wkstr2 As String
        Dim endpos As Integer
        For Each treenode In basenode.Nodes

            wkStr = treenode.text
            wkstr2 = ":"
            endpos = wkStr.IndexOf(wkstr2)
            wkStr = Trim(wkStr.Remove(endpos))

            If InStr(wkStr, ":") < 1 Then
                wkStr = wkStr & ":"
            End If

            treenode.text = wkStr


        Next


    End Sub




Usage
        'will loop through each parent/root node 
        For intX = 0 To FrmCreateCQSummary.tvSummary.Nodes.Count - 1
            clearSummaryValues(FrmCreateCQSummary.tvSummary.Nodes(intX))
        Next


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1