Results 1 to 2 of 2

Thread: [RESOLVED] Finding Node in Treeview - How?

  1. #1

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Resolved [RESOLVED] Finding Node in Treeview - How?

    I am attempting to find a particular node in a Treeview so that I can add nodes to that particular node. But when I try to loop through the nodes I am only getting the Root node. Here is the code I am using:

    VB Code:
    1. Private Sub AddNodes()
    2.         Dim tnode As TreeNode 'Treeview Node
    3.         Dim a As Integer 'Counter
    4.  
    5.         tnode = Me.TreeView1.Nodes.Add("Root Node", "Root", 14)
    6.         Selectednode = tnode
    7.  
    8.         AddChild(Selectednode, "Child 1", 1)
    9.         AddChild(Selectednode, "Child 2", 2)
    10.         AddChild(Selectednode, "Child 3", 3)
    11.         AddChild(Selectednode, "Child 4", 4)
    12.         AddChild(Selectednode, "Child 5", 5)
    13.         AddChild(Selectednode, "Child 6", 6)
    14.  
    15.         a = 0
    16.  
    17.         With TreeView1
    18.             For Each tnode In .Nodes '<==The root node is the only node in collection
    19.                 With .Nodes
    20.                     If .Item(a).Text = "Child 3" Then 'Add Gr Child Node to Child 3
    21.                         Selectednode = .Item(a)
    22.                         AddChild(Selectednode, "Grand Child 1", 12)
    23.                         AddChild(Selectednode, "Grand Child 2", 13)
    24.                         Exit For 'Exit Loop if Found
    25.                     End If
    26.                 End With
    27.                 a = a + 1 'Increment Counter
    28.             Next
    29.         End With
    30.     End Sub

    Any ideas?
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  2. #2

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Finding Node in Treeview - How?

    I figured it out just as I was pressing the submit button. I need to change the scope. Here is the code:


    VB Code:
    1. With TreeView1.Node(0) '<===I needed to add the Nodes(0)
    2.             For Each tnode In .Nodes 'Num of nodes attached to root node collection
    3.                 With .Nodes
    4.                     If .Item(a).Text = "Child 3" Then 'Add Gr Child Node to Child 3
    5.                         Selectednode = .Item(a)
    6.                         AddChild(Selectednode, "Grand Child 1", 12)
    7.                         AddChild(Selectednode, "Grand Child 2", 13)
    8.                         Exit For 'Exit Loop if Found
    9.                     End If
    10.                 End With
    11.                 a = a + 1 'Increment Counter
    12.             Next
    13.         End With
    Last edited by Mark Gambo; Jan 1st, 2006 at 04:16 AM.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


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