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
|