Library tutorials & articles

TreeView Control

Adding Nodes

Adding an item, or Node to the TreeView control is simple, once you know how. Adding a node uses the following syntax:

TreeView1.Nodes.Add Relative, Relationship, Key, Text, Image, SelectedImage

All of this parameters are optional. As the TreeView control is made up of items in a Tree, Visual Basic needs to know where the new item goes. You use Relative and Relationship to specify this. Relative specifies a relation (an existing node) to the new node you are creating. Relationship specifies how the new node is related to this existing node. Possible relationships are:

tvwChild '// new node is a child (one level down) of the specified node
tvwFirst '// new node is first, at the same level of the specified node
tvwLast '// new node is last, at the same level of the specified node
tvwNext '// new node is after (at the same level) the specified node
tvwPrevious '// new node is before (at the same level) the specified node

Key is a unique indentifier for this node (but easier to remember than an Index ID), which can be a string. Text is the text to be displayed for this node. Image is the image to be displayed next to the node, specified from the bound Image List (discussed later), such as a folder. SelectedImage is the image to be displayed when the node is selected, such as an open folder, specified from the bound Image list (discussed later). Add a TreeView control to your form and enter the code below.

Private Sub Form_Load()
    '// add some items
    TreeView1.Nodes.Add , , "root", "Root Item"
    TreeView1.Nodes.Add "root", tvwChild, "child1_root", "Child1 of Root"
    TreeView1.Nodes.Add "root", tvwChild, "child2_root", "Child2 of Root"
    TreeView1.Nodes.Add "child1_root", tvwChild, "child1_child1", "Child1 of Child1"
    '// make sure Child1 of Child1 is visible
    TreeView1.Nodes("child1_child1").EnsureVisible
End Sub

Run the project. You should see something like the image below:

Notice that the last line in the Form_Load procedure uses the EnsureVisible method. This forces the specified node to become visible, by expanding another item in needed.

Comments

  1. 03 Mar 2005 at 21:58

    I tried your treeview example and when I try to add and image it is telling me that I need to initilaize the image first. Can you tell me what that means and how to fix it

  2. 28 May 2004 at 10:39

    I tried this and recieved the following: Compile error: variable not defined..."TreeView" is highlited...anyone have any ideas?

  3. 14 May 2004 at 17:19
    Very Good, You have helped me.

    gabriel medina
    gxsoft@hotmail.com
  4. 11 Aug 2003 at 20:25
    Good Example isn't it?
    http://www.pldental.com
  5. 24 Jun 2003 at 03:39

    Hi,


    Found this very good tutorial because its nearly exact the problem i have with my TreeView,...
    ...but unfortunatly, i can't get it to work...


    Set the same settings like in the tutorial, but nothing happens.
    I try to drag and drop, but it seems, there is no action by the script,...


    Any suggetions, what my failure could be?
    Thanx for help
    LittleMik

  6. 08 May 2003 at 16:40

    Enjoyed the piece on drag and drop treeview.  
    I use VS6 under XP and in that environment the DataObject declaration is
    Data As MSComctl2.DataObject
    rather than the reference in the posting
    Hyperlink to articles on DataSource and imagelist would be handy but they are both documented in MSDN if you throw the rigth incantation at it.


    Nigel

  7. 04 Mar 2003 at 23:00

    Just what i needed for a contact list im doing for my MSN Clone. Excellent!

  8. 30 Oct 2002 at 09:53

    Can this be implemented using ASP pages as well?


    Thanks.

  9. 15 Oct 2002 at 00:44
    Thank You Example I make this!!

    I hope someOne found this useful

    Private Sub Form_Load()
    With TreeView1
           .Style = tvwTreelinesPlusMinusPictureText
           .LineStyle = tvwRootLines
           .PathSeparator = "\"
           .Indentation = Screen.TwipsPerPixelX * 5 '256
           .LabelEdit = tvwManual
           .SingleSel = False
           .HideSelection = False
           .ImageList = ImageList1        
           .Refresh
    End With
    Call ShowEstructure
    End Sub



    Private Sub ShowEstructure()
    Dim i As Byte
    Dim j As Byte
    Dim k As Byte
    Dim Max As Byte
    Dim sp As String
    Dim sh As String
    Dim sh2 As String
    Dim TotalSecciones As Byte
    Dim TotalPosiciones As Byte

    Dim rsSeccions As New ADODB.Recordset
    Dim rsPositions As New ADODB.Recordset

    TreeView1.Nodes.Clear


    Set grsGral = New ADODB.Recordset
    grsGral.Open "EXEC spSQL_ShowDepartments", gConnectionString, adOpenStatic, adLockReadOnly, 1
    Max = grsGral.RecordCount


    For i = 1 To Max
           'Actual Node        
            sp = "nodo" & CStr(i)
           
           TreeView1.Nodes.Add , , sp, grsGral.Fields(1).Value, 1
           grsGral.MoveNext
    Next
    grsGral.MoveFirst

    For i = 1 To Max
     ' leaf
           sp = "nodo" & CStr(i)
           Set rsSeccions = New ADODB.Recordset
           rsSeccions.Open "EXEC spSQL_ShowSeccions_forThis '" & grsGral.Fields(0).Value & "'", gConnectionString, adOpenStatic, adLockReadOnly, 1
           TotalSecciones = rsSeccions.RecordCount
           
           For j = 1 To TotalSecciones
               ' relative son
               sh = sp & "-" & CStr(j) 'j
               TreeView1.Nodes.Add sp, tvwChild, sh, rsSeccions.Fields(0).Value, 2
               
               '*-*-*-
               
                Set rsPositions = New ADODB.Recordset
                   rsPositions.Open "EXEC spSQL_ShowPositions_forThis '" & rsSeccions.Fields(1).Value & "'", gConnectionString, adOpenStatic, adLockReadOnly, 1
                   TotalPosiciones = rsPositions.RecordCount
                   
                   For k = 1 To TotalPosiciones
                       ' La clave del nodo hijo, relativo al nodo sP
                       sh2 = sh & "-" & CStr(k) 'j
                       TreeView1.Nodes.Add sh, tvwChild, sh2, rsPositions.Fields(0).Value, 3
                       rsPositions.MoveNext
                   Next
                   Set rsPositions = Nothing
               '*--*-*-
               
               rsSeccions.MoveNext
           Next
           Set rsSeccions = Nothing
           grsGral.MoveNext
    Next

    Set grsGral = Nothing
    End Sub

  10. 27 May 2002 at 15:08

    Hoped to find some example using directories and files...

  11. 02 Apr 2002 at 12:37

    Sorry, I went back and read it more closely.  I just had to play around with the ImageList control to make it work.

  12. 02 Apr 2002 at 12:33

    I might have missed it, but I would have liked to have seen how to use images without having to go to the imagelist tutorial.  I know you mentioned imagelist, but perhaps a brief explanation of using the imagelist control with TreeView would have been in order.  If I missed it, I apologize.  Thank you for a very helpful tutorial.

  13. 01 Jan 1999 at 00:00

    This thread is for discussions of TreeView Control.

Leave a comment

Sign in or Join us (it's free).

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!