Results 1 to 5 of 5

Thread: [RESOLVED] Treeview Add Node Function

Hybrid View

  1. #1
    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

  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

    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