Results 1 to 5 of 5

Thread: [RESOLVED] Treeview Add Node Function

  1. #1

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

    Resolved [RESOLVED] Treeview Add Node Function

    I am attempting to write a sub that will add a node to a treeview:


    VB Code:
    1. Private Sub AddNode(ByVal tvwName As TreeView, _
    2.                         ByVal strText As String, ByVal intImage As Integer, _
    3.                         Optional ByVal intSelectImage As Integer = 99999)
    4.  
    5.         Dim tvwNode As New TreeNode
    6.  
    7.         With tvwNode
    8.             .Text = strText
    9.             .ImageIndex = intImage
    10.             If intSelectImage <> 99999 Then .SelectedImageIndex = intSelectImage Else .SelectedImageIndex = intImage
    11.         End With
    12.  
    13.         tvwName.Nodes.Add(tvwNode)
    14.     End Sub
    15.  
    16.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    17.  
    18.         Call AddNode(TreeView1, "Text1", 8)
    19.  
    20.     End Sub

    How can I pass the node to this sub to which I want to add a child node to?
    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: Treeview Add Node Function

    Basically here is the VB6 sub I am trying to recreate:

    VB Code:
    1. Public Sub tvwNode_Add(tvwName As TreeView, _
    2.                             tvwParent As String, _
    3.                             tvwRelationship As String, _
    4.                             tvwKey As String, _
    5.                             tvwText As String, _
    6.                             Optional tvwTag As String, _
    7.                             Optional ByVal tvwImage As Long, _
    8.                             Optional ByVal tvwImageSel As Long, _
    9.                             Optional tvwEnsureVisible As Boolean = True)
    10. Dim nodX As Node
    11.  
    12.     With tvwName
    13.         With .Nodes
    14.             Set nodX = .Add(tvwParent, tvwRelationship, tvwKey, tvwText, tvwImage, tvwImageSel)
    15.                 nodX.Tag = tvwTag & ""
    16.         End With
    17.        
    18.             If tvwEnsureVisible Then
    19.                 .Nodes(tvwKey).EnsureVisible
    20.             End If
    21.     End With
    22.    
    23. Set nodX = Nothing
    24. End Sub

    as well as this one too

    VB Code:
    1. Public Sub tvwNode_Edit(tvwName As TreeView, _
    2.                             tvwIndex As Long, _
    3.                             tvwText As String, _
    4.                             Optional ByVal tvwKey As String, _
    5.                             Optional ByVal tvwTag As String, _
    6.                             Optional ByVal tvwTextColor As Long = vbBlack)
    7.    
    8. On Error GoTo tvwNode_Edit_Error
    9.  
    10.     With tvwName.Nodes.Item(tvwIndex)
    11.         .Key = tvwKey
    12.         .ForeColor = tvwTextColor
    13.         .Text = tvwText
    14.         .Tag = tvwTag
    15.     End With
    16.  
    17. On Error GoTo 0
    18. Exit Sub
    19.  
    20. tvwNode_Edit_Error:
    21. If Err.Number = 35603 Then
    22.     Exit Sub
    23. Else
    24.     MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure tvwNode_Edit of Module "
    25. End If
    26. End Sub
    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

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

    Re: Treeview Add Node Function

    ** Bump **
    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."


  4. #4
    Junior Member
    Join Date
    Apr 2005
    Posts
    30

    Re: Treeview Add Node Function

    Hi
    I wrote this example to give you some ideas. i knocked it up quickly

    But it's not the usual way of displaying a tree. Usually you are displaying some data and you would use some re-entrant code.eg (recursion)

    just paste this onto a form with 3 buttons and a treeview

    Play around with selecting nodes!

    hope it helps

    Mike Pooley

    VB Code:
    1. Public Class Form1
    2.     Dim Selectednode As TreeNode
    3.  
    4.  
    5.  
    6.     Private Sub AddSibling(ByVal CurrentNode As TreeNode, _
    7.                    ByVal strText As String, ByVal intImage As Integer, _
    8.                    Optional ByVal intSelectImage As Integer = 99999)
    9.         Dim tvwnode As New TreeNode
    10.         With tvwNode
    11.             .Text = strText
    12.             .ImageIndex = intImage
    13.             If intSelectImage <> 99999 Then .SelectedImageIndex = intSelectImage Else .SelectedImageIndex = intImage
    14.         End With
    15.         If CurrentNode.Level = 0 Then
    16.             TreeView1.Nodes.Add(tvwnode)
    17.         Else
    18.             CurrentNode.Parent.Nodes.Add(tvwnode)
    19.         End If
    20.  
    21.  
    22.     End Sub
    23.     Private Sub AddChild(ByVal CurrentNode As TreeNode, _
    24.                    ByVal strText As String, ByVal intImage As Integer, _
    25.                    Optional ByVal intSelectImage As Integer = 99999)
    26.         Dim tvwnode As New TreeNode
    27.         With tvwnode
    28.             .Text = strText
    29.             .ImageIndex = intImage
    30.             If intSelectImage <> 99999 Then .SelectedImageIndex = intSelectImage Else .SelectedImageIndex = intImage
    31.         End With
    32.         CurrentNode.Nodes.Add(tvwnode)
    33.         TreeView1.ExpandAll()
    34.     End Sub
    35.  
    36.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    37.  
    38.         Call AddSibling(Selectednode, "sibling ", 8)
    39.     End Sub
    40.  
    41.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    42.         AddChild(Selectednode, "Child", 8)
    43.     End Sub
    44.  
    45.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    46.         Dim rootnode As TreeNode
    47.         rootnode = Me.TreeView1.Nodes.Add("RootNode", "Root")
    48.         Selectednode = rootnode
    49.     End Sub
    50.  
    51.     Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
    52.         Selectednode = e.Node
    53.     End Sub
    54. End Class

  5. #5

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

    Re: Treeview Add Node Function

    mikepy,

    Thanks for the code! It make a little more sense to me now.
    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