|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
When you first add a tree view control, there are only sample nodes visible in it, and nothing at runtime. Youre responsible for adding the nodes and setting up their relationships, text, and images yourself. Well do that in the topics that follow in this chapter, but for reference, we list the program well develop here, so you can refer back to it as you like. Running this example program yields the results you see in Figure 16.8; as you can see, we let the user expand and collapse nodes in the tree view, have associated both an image and text with each node, and report which node was clicked in a text box at the bottom on the form. This program has the following controls in it: an image list, ImageList1 ; a tree view control, TreeView1 , with its Style property set to 7 (the default); and a text box, Text1.
The code for this example is located in the treeview folder on this books accompanying CD-ROM. Selecting Tree View StylesThere are many different styles for tree viewstext nodes only, pictures and text nodes, showing or not showing the tree lines that connect nodes, showing or not showing the plus and minus symbols to expand or collapse nodes, and so on. You set the tree views style using its Style property. Here are the possible values (well stick to the default, style 7, tvwTreelinesPlusMinusPictureText , in this chapter because that style offers the richest set of attributes):
Adding Nodes To A Tree ViewThe Testing Department is calling again. The tree view youve added to your program is fine, but why isnt there anything in it? Oops, you think, its time to add some nodes. You actually add Node objects to a tree view by adding them to the Nodes collection. How does this work? Lets see an example. Here, well add a node, Node1 , to a tree view, TreeView1 (the tree views Style property is set to tvwTreelinesPlusMinusPictureText , the default). First, we declare that node:
Private Sub Form_Load()
Dim Node1 As Node
...
Next, we add the node to the tree view using the Nodes collections Add method (see the next topic for more information on this method):
Private Sub Form_Load()
Dim Node1 As Node
Set Node1 = TreeView1.Nodes.Add
...
Now we can refer to the node by name, Node1 , as we set its text:
Private Sub Form_Load()
Dim Node1 As Node
Set Node1 = TreeView1.Nodes.Add
Node1.Text = "Node 1"
...
We can also refer to the node as a member of the Nodes collection as here, where we set the nodes Key property:
Private Sub Form_Load()
Dim Node1 As Node
Set Node1 = TreeView1.Nodes.Add
Node1.Text = "Node 1"
TreeView1.Nodes(1).Key = "Node 1"
End Sub
How does this look when you run it? You can see the result in Figure 16.9: not very spectacular with just one node. You can add other nodes by duplicating the preceding code and naming the new nodes Node2, Node3, and so on, but theyll all appear at the same level. Arent trees supposed to have nodes that contain other nodes? They are, and well take a look at that in the next topic.
Adding Subnodes To A Tree ViewThe Testing Department is calling again. The new node youve put in your tree view is nice, but dont tree views usually display more than one node? What about other nodes and nodes that contain subnodes? Ok, you say, no problem. When you add a new node to a tree views Nodes collection using the Add method, you can specify how it is related to the nodes already there. Heres how you use the Add method in general: Nodes.Add(relative , [ relationship ] [, key ] [, text ] [, image ] [, selectedimage ]) The relative argument is another node that youre relating the new node to with the relationship argument. Here are the possible values for relationship:
Lets see an example. In this case, well set up the tree of text nodes, with one root node that has two nodesand the second of those subnodes has a subnode itself. In this example, well use a tree view control, TreeView1 , (the tree views Style property is set to tvwTreelinesPlusMinusPictureText , the default) and add four new nodes, Node1 to Node4 :
Private Sub Form_Load()
Dim Node1, Node2, Node3, Node4 As Node
...
We add the first node like this using the Nodes collections Add method:
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"
...
Now we add two nodes, Node2 and Node3 , that are child nodes of the first node:
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"
...
|
|
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. |