|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Finally, we add a fourth node, Node4 , which is the child of Node3 :
Private Sub Form_Load()
Dim Node1, Node2, Node3, Node4 As Node
Set Node1 = TreeView1.Nodes.Add
TreeView1.Nodes(1).Text = "Node 1"
TreeView1.Nodes(1).Key = "Node 1"
Set Node2 = TreeView1.Nodes.Add("Node 1", tvwChild, "Node 2")
TreeView1.Nodes(2).Text = "Node 2"
TreeView1.Nodes(2).Key = "Node 2"
Set Node3 = TreeView1.Nodes.Add("Node 1", tvwChild, "Node 3")
TreeView1.Nodes(3).Text = "Node 3"
TreeView1.Nodes(3).Key = "Node 3"
Set Node4 = TreeView1.Nodes.Add("Node 3", tvwChild, "Node 4")
TreeView1.Nodes(4).Text = "Node 4"
TreeView1.Nodes(4).Key = "Node 4"
End Sub
And thats itthe result appears in Figure 16.10. Now were adding nodes and subnodes to a tree view control.
Adding Images To A Tree ViewThe Aesthetic Design Department is on the phone. About that tree view control in your programcant you give each node an image? All the other Windows programs seem to do that. You look around, note that the tree view Node objects have an Image property, and say, no problem. To add an image to a node in a tree view, you just have to set its Image property to an index or key in the tree views associated image list control. Lets see an example. Here, well use an image list control, ImageList1 , with two images taken from the Visual Basic common\graphics\bitmaps\outline directory: closed.bmp and leaf.bmp, which we add to the image list control with the Key properties closed and leaf, respectively, as shown in Figure 16.11.
Now we can add those images to the nodes in a tree view control, TreeView1 , by using the Node objects Image property, setting that property to the Key values for the various images:
Private Sub Form_Load()
Dim Node1, Node2, Node3, Node4 As Node
Set Node1 = TreeView1.Nodes.Add
TreeView1.Nodes(1).Text = "Node 1"
TreeView1.Nodes(1).Key = "Node 1"
TreeView1.Nodes(1).Image = "closed"
Set Node2 = TreeView1.Nodes.Add("Node 1", tvwChild, "Node 2")
TreeView1.Nodes(2).Text = "Node 2"
TreeView1.Nodes(2).Key = "Node 2"
TreeView1.Nodes(2).Image = "leaf"
Set Node3 = TreeView1.Nodes.Add("Node 1", tvwChild, "Node 3")
TreeView1.Nodes(3).Text = "Node 3"
TreeView1.Nodes(3).Key = "Node 3"
TreeView1.Nodes(3).Image = "closed"
Set Node4 = TreeView1.Nodes.Add("Node 3", tvwChild, "Node 4")
TreeView1.Nodes(4).Text = "Node 4"
TreeView1.Nodes(4).Key = "Node 4"
TreeView1.Nodes(4).Image = "leaf"
End Sub
The result appears in Figure 16.12now were adding images to tree view nodes in Visual Basic.
However, if you take a close look at Figure 16.12, youll see that the folders there are closed, even when the node they represent is open. How can we change those images to an open folder when the user expands a node? For the details, see the next topic. Expanding And Collapsing Nodes (And Setting Node Images To Match)When the user clicks a plus or minus sign in a tree view to expand or contract a node, how can we make the nodes image match? For example, when the node is closed, we can display a closed folder image, and when expanded, an open folder image. Well take those images from the Visual Basic common\graphics\ bitmaps\outline directory: open.bmp and closed.bmp. Add those images to an image list, ImageList1 , now, giving them the Key properties open and closed. Next, connect the image list control to a tree view control, TreeView1 , by setting that controls ImageList property to ImageList1. When the user closes a node, the tree view control generates a Collapse event: Private Sub TreeView1_Collapse(ByVal Node As ComctlLib.Node) End Sub In that events handler, we can set the nodes image to the closed folder by referring to that image by its key:
Private Sub TreeView1_Collapse(ByVal Node As ComctlLib.Node)
Node.Image = "closed"
End Sub
Similarly, when the user expands a node, the tree view control generates an Expand event: Private Sub TreeView1_Expand(ByVal Node As ComctlLib.Node) End Sub In that events handler, we set the nodes image to the open folder:
Private Sub TreeView1_Expand(ByVal Node As ComctlLib.Node)
Node.Image = "open"
End Sub
Thats all it takesnow the nodes in this program display open and closed folders when they are expanded and collapsed, as shown in Figure 16.13.
Handling Tree View Node ClicksHow do you know which node in a tree view the user clicked? You can use the NodeClick event: Private Sub TreeView1_NodeClick(ByVal Node As ComctlLib.Node) End Sub For example, we can display the text in the node that the user has clicked in a text box, Text1, this way:
Private Sub TreeView1_NodeClick(ByVal Node As ComctlLib.Node)
Text1.Text = "You clicked " & Node.Text
End Sub
The result of this code appears in Figure 16.14when the user clicks a node, the program indicates which node was clicked in the text box at the bottom. Now were handling tree view node clicks in Visual Basic.
Adding A List View To A FormThe Testing Department is calling again. When you list all files on disk in a text box in your SuperDuperTextPro program, doesnt that text box seem pretty full? Of course, you say, there are hundreds of filenames to display. Try a list view control, they say. To add a list view control to a form, follow these steps:
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |