Visual Basic Common Control API Routines
Pure VB: Link a TreeView and Listview Together via their Tag Properties
     
Posted:   Sunday June 21, 1998
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB5, Windows 98
OS restrictions:   None
Author:   VBnet - Randy Birch
     
 Prerequisites
None.

Unlike most of the other TreeView and listview code examples here on VBnet, this one doesn't use APIs. Its creation was prompted by a newsgroup question by someone wanting to have a TreeView and listview with similar items, and to behave such that when the user selected a TreeView item, the corresponding listview item was selected as well.

Working this up, I decided to set both the TreeView and listview keys and tags to the same value for the same data. This made locating matching items easy using the ListView's FindItem method with lvwTag as the search criteria.
 BAS Module Code
None.

 Form Code
Add a TreeView and ListView control to the form, and two command buttons (Command1 and Command2).  Paste the following code into the General Declarations area of the form:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Command2_Click()

   Unload Me
   
End Sub


Private Sub Command1_Click()

   Dim itmX As ListItem
   Dim nodX As Node
   Dim item As String
   
   On Local Error GoTo error_create
   
  'add a few items to the treeview
   Set nodX = TreeView1.Nodes.Add(, , "r1", "Root 1")
   nodX.Expanded = True
   
   Set nodX = TreeView1.Nodes.Add(, , "r2", "Root 2")
   nodX.Expanded = True
   
   Set nodX = TreeView1.Nodes.Add(, , "r3", "Root 3")
   nodX.Expanded = True
   
   Set nodX = TreeView1.Nodes.Add("r1", _
                                  tvwChild, "child1", _
                                  "Child")
   nodX.Expanded = True
   
   Set nodX = TreeView1.Nodes.Add("child1", _
                                  tvwChild, "level2ch1", _
                                  "Parent is child1")
   nodX.Expanded = True
   
   Set nodX = TreeView1.Nodes.Add("r2", _
                                  tvwChild, "child2", _
                                  "Child #2")
   nodX.Expanded = True
   
   Set nodX = TreeView1.Nodes.Add("child2", _
                                  tvwChild, "level2ch2", _
                                  "Parent is child2")
   nodX.Expanded = True
   
'-----------------------------------------
   Dim tvText As String
   Dim tvKey As String

  'now add the same items to the listview, by
  'retrieving the treeview item's text and tag
   tvText = TreeView1.Nodes(1).Text
   tvKey = TreeView1.Nodes(1).Key
   Set itmX = ListView1.ListItems.Add(, tvKey, tvText)
   
  'assign the treeview items tag to the listitem
   itmX.Tag = tvKey
   
  'and for debugging, display that tag as a subitem
   itmX.SubItems(1) = tvKey
   
   tvText = TreeView1.Nodes(2).Text
   tvKey = TreeView1.Nodes(2).Key
   Set itmX = ListView1.ListItems.Add(, tvKey, tvText)
   itmX.Tag = tvKey
   itmX.SubItems(1) = tvKey
   
   tvText = TreeView1.Nodes(3).Text
   tvKey = TreeView1.Nodes(3).Key
   Set itmX = ListView1.ListItems.Add(, tvKey, tvText)
   itmX.Tag = tvKey
   itmX.SubItems(1) = tvKey
   
   tvText = TreeView1.Nodes(4).Text
   tvKey = TreeView1.Nodes(4).Key
   Set itmX = ListView1.ListItems.Add(, tvKey, tvText)
   itmX.Tag = tvKey
   itmX.SubItems(1) = tvKey
   
   tvText = TreeView1.Nodes(5).Text
   tvKey = TreeView1.Nodes(5).Key
   Set itmX = ListView1.ListItems.Add(, tvKey, tvText)
   itmX.Tag = tvKey
   itmX.SubItems(1) = tvKey
   
   tvText = TreeView1.Nodes(6).Text
   tvKey = TreeView1.Nodes(6).Key
   Set itmX = ListView1.ListItems.Add(, tvKey, tvText)
   itmX.Tag = tvKey
   itmX.SubItems(1) = tvKey
   
   ListView1.ColumnHeaders(1).Text = "Treeview Item"
   ListView1.ColumnHeaders(2).Text = "TV/LV Tag"

   Exit Sub
   
error_create:
     
   MsgBox "Sorry... the controls are already populated!"
   
End Sub


Private Sub TreeView1_NodeClick(ByVal Node As ComctlLib.Node)

   Dim tvKey
   Dim lvIndex As Long
   Dim itmFound As ListItem
   
  'display the tag and index selected
   Label1.Caption = TreeView1.Nodes(TreeView1.SelectedItem.Index).Key & _
                    "  Index: " & _
                    TreeView1.Nodes(TreeView1.SelectedItem.Index).Index
   
  'retrieve the treeview tag for the selected item
   tvKey = TreeView1.Nodes(TreeView1.SelectedItem.Index).Key
   
  'attempt to locate the corresponding item in the listview
   Set itmFound = ListView1.FindItem(tvKey, lvwTag, , 0)
   
  'and react accordingly
   If itmFound Is Nothing Then
      ListView1.SelectedItem.Selected = False
      MsgBox "The listview does not contain this item!"  
   Else
      itmFound.EnsureVisible
      itmFound.Selected = True
   End If

End Sub


Private Sub ListView1_ColumnClick(ByVal ColumnHeader _
                                  As ComctlLib.ColumnHeader)

   Static sorder As Integer
   
   sorder = Not sorder
   ListView1.SortKey = ColumnHeader.Index - 1
   ListView1.SortOrder = Abs(sorder)
   ListView1.Sorted = True
   
End Sub
 Comments
Run the project. Selecting a treeview item will select the matching listview item. If the item "Parent is child2" is selected, a message box appears indicating it is not in the listview

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter