|
|
Title | Use TreeView and ListView controls together |
Description | This example shows how to use the TreeView and ListView controls together in Visual Basic 6. When you select a person node in the TreeView control, the program's ListView control displays extra information about that person. |
Keywords | TreeView, ListView |
Categories | Controls |
|
|
The EmployeeProjects class contains a ProjectNames collection that stores the projects that an empolyee is working on. Subroutine AddProjects adds a new EmployeeProjects object to the Projects collection. It takes as parameters an employee's name and a parameter list of project names. It makes a new EmployeeProjects object and adds the project names to the ProjectNames collection. It then adds the EmployeeProjects object to the Projects collection using the person's name as its key.
|
|
' Save information about these projects for this
' employee.
Private Sub AddProjects(emp_name As String, ParamArray _
proj_names() As Variant)
Dim emp_proj As New EmployeeProjects
Dim i As Integer
' Add the project names to the EmplyeeProjects
' object for this employee.
For i = LBound(proj_names) To UBound(proj_names)
emp_proj.ProjectNames.Add proj_names(i)
Next i
' Add emp_proj to the collection of EmployeeProjects.
Projects.Add emp_proj, emp_name
End Sub
|
|
When you press on a TreeView node, the OrgTree_MouseDown saves a reference to the node you are pressing.
When you release the mouse, the OrgTree_Click event handler uses the node's key to look up the corresponding EmployeeProjects object in the Projects collection. If such an object exists, the program displays its project names in the TreeView control.
|
|
Private Sub OrgTree_MouseDown(Button As Integer, Shift As _
Integer, x As Single, y As Single)
Set SourceNode = OrgTree.HitTest(x, y)
End Sub
' Display the data for this node in the ListView.
Private Sub OrgTree_Click()
Dim emp_proj As EmployeeProjects
Dim column_header As ColumnHeader
Dim list_item As ListItem
Dim obj As Object
Dim i As Integer
' Clear the ListView.
lvProjects.ListItems.Clear
lvProjects.ColumnHeaders.Clear
' Get the employee's EmployeeProjects object.
If SourceNode Is Nothing Then Exit Sub
On Error GoTo NoProjects
Set emp_proj = Projects(SourceNode.Key)
On Error GoTo 0
' Fill in the ListView.
' Create the column header.
Set column_header = lvProjects. _
ColumnHeaders.Add(, , "Project", _
2 * TextWidth("Project"))
With emp_proj.ProjectNames
For i = 1 To .Count
Set list_item = lvProjects.ListItems.Add(, , _
.Item(i))
list_item.Icon = 1
list_item.SmallIcon = 1
Next i
End With
NoProjects:
Exit Sub
End Sub
|
|
This program also includes drag and drop features that lets you rearrange the TreeView's nodes.
|
|
|
|
|
|