Results 1 to 10 of 10

Thread: [RESOLVED] Adding a Node to a Child Node - How?

  1. #1

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

    Resolved [RESOLVED] Adding a Node to a Child Node - How?

    I am adding a child node (Node 1) to a treeview and then I want to add a Child Node (Node 2) to the newly added child node (Node 1):

    VB Code:
    1. AddChild(Selectednode, "John Doe", 12) 'Adding Node1
    2.  
    3. intNodeNum = TreeView1.Nodes(0).Nodes.Count + 1 ' I am attempting to get the
    4.                                                 ' index of last added node
    5.  
    6. Selectednode = TreeView1.Nodes(0).Nodes(intNodeNum) ' <== I get an error on this line
    7.  
    8. AddChild(Selectednode, "Address", 13) ' <==Node 2

    How do I do this?
    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: Adding a Node to a Child Node - How?

    I tried using the following:

    VB Code:
    1. TreeView1.SelectedNode = TreeView1.Nodes(0).PrevNode

    Thinking that it would given me the index of the last node added (Node 1) but no dice.
    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."


  3. #3
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Adding a Node to a Child Node - How?

    Quote Originally Posted by Mark Gambo
    I am adding a child node (Node 1) to a treeview and then I want to add a Child Node (Node 2) to the newly added child node (Node 1):

    VB Code:
    1. AddChild(Selectednode, "John Doe", 12) 'Adding Node1
    2.  
    3. intNodeNum = TreeView1.Nodes(0).Nodes.Count + 1 ' I am attempting to get the
    4.                                                 ' index of last added node
    5.  
    6. Selectednode = TreeView1.Nodes(0).Nodes(intNodeNum) ' <== I get an error on this line
    7.  
    8. AddChild(Selectednode, "Address", 13) ' <==Node 2

    How do I do this?
    Hi Mark,

    Why you want to do it by code?
    If you go via TOOLBOX -> select TREEVIEW and create it on your Form,
    then go to the properties of your TREEVIEW and select NODES and you can
    Add Root and Add Cilds as many as you need.

    Happy New Year,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  4. #4

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

    Re: Adding a Node to a Child Node - How?

    Quote Originally Posted by sparrow1
    Hi Mark,

    Why you want to do it by code?
    If you go via TOOLBOX -> select TREEVIEW and create it on your Form,
    then go to the properties of your TREEVIEW and select NODES and you can
    Add Root and Add Cilds as many as you need.

    Happy New Year,

    sparrow1
    Happy New Year to you!!!

    I want to be able to add the nodes during run time, specificaly when a User adds a name to a particular node (Node 1) I want to automatically add a child node to the newly created node (Node 2).

    Thanks!
    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."


  5. #5
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Adding a Node to a Child Node - How?

    I thought so after I saw your second thread about Treeviews.
    I'll try it.

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  6. #6

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

    Re: Adding a Node to a Child Node - How?

    Quote Originally Posted by sparrow1
    I thought so after I saw your second thread about Treeviews.
    I'll try it.

    Wkr,

    sparrow1
    Thanks
    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."


  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding a Node to a Child Node - How?

    Why don't you have the AddChild method return a reference to the newly added node? That way you can simply pass that reference as the first argument to the next call to AddChild.

    As to your error, the index of the last node is Count-1, not Count+1.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

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

    Re: Adding a Node to a Child Node - How?

    Quote Originally Posted by jmcilhinney
    Why don't you have the AddChild method return a reference to the newly added node? That way you can simply pass that reference as the first argument to the next call to AddChild.

    As to your error, the index of the last node is Count-1, not Count+1.

    Thanks for the post. The following code is my AddChild Sub:

    VB Code:
    1. Private Sub AddChild(ByVal CurrentNode As TreeNode, _
    2.                    ByVal strText As String, ByVal intImage As Integer, _
    3.                    Optional ByVal intSelectImage As Integer = 99999)
    4.         Dim tvwnode As New TreeNode
    5.         With tvwnode
    6.             .Text = strText
    7.             .ImageIndex = intImage
    8.             If intSelectImage <> 99999 Then .SelectedImageIndex = intSelectImage Else .SelectedImageIndex = intImage
    9.         End With
    10.         CurrentNode.Nodes.Add(tvwnode)
    11.         TreeView1.ExpandAll()
    12.     End Sub

    How would I alter this sub in order to return a reference to the newly added node?

    Thanks!
    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."


  9. #9
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: Adding a Node to a Child Node - How?

    VB Code:
    1. Private [b]Function[/b] AddChild(ByVal CurrentNode As TreeNode, _
    2.                    ByVal strText As String, ByVal intImage As Integer, _
    3.                    Optional ByVal intSelectImage As Integer = 99999) [b]As TreeNode[/b]
    4.         Dim tvwnode As New TreeNode
    5.         With tvwnode
    6.             .Text = strText
    7.             .ImageIndex = intImage
    8.             If intSelectImage <> 99999 Then .SelectedImageIndex = intSelectImage Else .SelectedImageIndex = intImage
    9.         End With
    10.         CurrentNode.Nodes.Add(tvwnode)
    11.  
    12.         'personally I would put this outside of this function
    13.         '[b]TreeView1.ExpandAll()[/b]
    14.  
    15.         'return value
    16.         Return tvwnode
    17.     End Sub
    Make a function out of it.
    "so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman

  10. #10

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

    Re: Adding a Node to a Child Node - How?

    Thanks, it worked like a charm!!
    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