Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


When you first add a tree view control, there are only sample nodes visible in it, and nothing at runtime. You’re responsible for adding the nodes and setting up their relationships, text, and images yourself. We’ll do that in the topics that follow in this chapter, but for reference, we list the program we’ll 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.


Figure 16.8  Using a tree view in a form.

The code for this example is located in the treeview folder on this book’s accompanying CD-ROM.

Selecting Tree View Styles

There are many different styles for tree views—text 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 view’s style using its Style property. Here are the possible values (we’ll stick to the default, style 7, tvwTreelinesPlusMinusPictureText , in this chapter because that style offers the richest set of attributes):

  tvwTextOnly—0
  tvwPictureText—1
  tvwPlusMinusText—2
  tvwPlusPictureText—3
  tvwTreelinesText—4
  tvwTreelinesPictureText—5
  tvwTreeLinesPlusMinusText—6
  tvwTreelinesPlusMinusPictureText—7 (the default)


TIP:  Note that you can set the tree view’s style at design time or runtime, which means you can allow users to customize the tree view’s appearance as they want.

Adding Nodes To A Tree View

The Testing Department is calling again. The tree view you’ve added to your program is fine, but why isn’t there anything in it? Oops, you think, it’s 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? Let’s see an example. Here, we’ll add a node, Node1 , to a tree view, TreeView1 (the tree view’s 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 collection’s 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 node’s 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 they’ll all appear at the same level. Aren’t trees supposed to have nodes that contain other nodes? They are, and we’ll take a look at that in the next topic.


Figure 16.9  Placing a node in a tree view.

Adding Subnodes To A Tree View

The Testing Department is calling again. The new node you’ve put in your tree view is nice, but don’t 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 view’s Nodes collection using the Add method, you can specify how it is related to the nodes already there. Here’s how you use the Add method in general:

Nodes.Add(relative , [ relationship ] [, key ] [, text ] [, image ]
[, selectedimage ])

The relative argument is another node that you’re relating the new node to with the relationship argument. Here are the possible values for relationship:

  tvwLast—1; the node is placed after all other nodes at the same level of the node named in relative.
  tvwNext—2; the node is placed after the node named in relative.
  tvwPrevious—3; the node is placed before the node named in relative.
  tvwChild—4; the node becomes a child node of the node named in relative.

Let’s see an example. In this case, we’ll set up the tree of text nodes, with one root node that has two nodes—and the second of those subnodes has a subnode itself.

In this example, we’ll use a tree view control, TreeView1 , (the tree view’s 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 collection’s 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"
...


Previous Table of Contents Next


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.