Public Sub InittreCategory()
On Error GoTo errhandler
Dim lnColCount As Integer
Dim lsCategoryCode As String
Dim lsCategoryDesc As String
Dim xnode As Node
Set colTableData = Nothing
Set colTableData = objCategory.ReadCategory
treCategory.ImageList = Me.imlCategory
Set xnode = Me.treCategory.Nodes.Add(, , "Category", "Category", "Root")
If colTableData Is Nothing Then
For lnColCount = 1 To colTableData.Count
lsCategoryCode = colTableData.Item(lnColCount).Item("CategoryCode")
lsCategoryDesc = colTableData.Item(lnColCount).Item("CategoryDesc")
If Len(lsCategoryCode) = 2 Then
Me.treCategory.Nodes.Add "Category", tvwChild, _
lsCategoryCode, lsCategoryDesc, "Code1Close", "Code1Open"
ElseIf Len(lsCategoryCode) = 4 Then
Me.treCategory.Nodes.Add Left(lsCategoryCode, 2), tvwChild, _
lsCategoryCode, lsCategoryDesc, "Code2", "Code2Seleted"
Set colTableData = Nothing
bOperatSuccess = True
Using the TreeView Control
The TreeView control is designed to display data that is hierarchical in nature, such as organization trees, the entries in an index, the files and directories on a disk.
Figure 2.40 Typical TreeView
To create an organization tree that can be manipulated by the user.
To create a tree that shows at least two or more levels of a database.
Setting Node Object Properties
A "tree" is comprised of cascading branches of "nodes," and each node typically consists of an image (set with the Image property) and a label (set with the Text property). Images for the nodes are supplied by an ImageList control associated with the TreeView control. For more information on using the ImageList control with other controls, see "Using the ImageList control."
A node can be expanded or collapsed, depending on whether or not the node has child nodes — nodes which descend from it. At the topmost level are "root" nodes, and each root node can have any number of child nodes. The total number of nodes is not limited (except by machine constraints). Figure 2.41 shows a tree with two root nodes; "Root 1" has three child nodes, and "Child 3" has a child node itself. "Root 2" has child nodes, as indicated by the "+" sign, but is unexpanded.
Figure 2.41 Root and child nodes
Each node in a tree is actually a programmable Node object, which belongs to the Nodes collection. As in other collections, each member of the collection has a unique Index and Key property which allows you to access the properties of the node. For example, the code below uses the Index of a particular node ("7") to set the Image and Text properties:
tvwMyTree.Nodes(7).Image = "closed"
tvwMyTree.Nodes(7).Text = "IEEE"
However, if a unique key, for example "7 ID" had been assigned to the node, the same code could be written as follows:
tvwMyTree.Nodes("7 ID").Image = "closed"
tvwMyTree.Nodes("7 ID").Text = "IEEE"
Node Relationships and References to Relative Nodes
Each node can be either a child or a parent, depending on its relationship to other nodes. The Node object features several properties which return various kinds of information about children or parent nodes. For example, the following code uses the Children property to return the number of children — if any — a node has:
However, some of the properties do not return information, as the Children property does, but instead return a reference to another node object. For example, the Parent property returns a reference to the parent of any particular node (as long as the node is not a root node). With this reference, you can manipulate the parent node by invoking any methods, or setting properties, that apply to Node objects. For example, the code below returns the Text and Index properties of a parent node:
Tip Use the Set statement with an object variable of type Node to manipulate references to other Node objects. For example, the code below sets a Node object variable to the reference returned by the Parent property. The code then uses the object variable to return properties of the relative node:
Dim tempNode As Node ' Declare object variable.
' Set object variable to returned reference.
Set tempNode = tvwMyTree.Nodes(10).Parent
MsgBox tempNode.Text ' Returns parent's Text.
MsgBox tempNode.Index ' Returns parent's Index.
Adding Node Objects to the Nodes Collection
To add a Node to the tree, use the Add method (Nodes collection). This method includes two arguments, relative and relationship, which can determine where the node will be added. The first argument relative names a node; the second argument relationship specifies the relationship between the new node and the node named in relative.
For example, the following code adds a node named "11 node" as a child of another node named "7 node." The intrinsic constant tvwChild specifies that the new node is a child of the node named in the previous argument. The third argument assigns the Key property to the new node.
tvwMyTree.Nodes.Add "7 node", tvwChild, "11 node"
Other possible relationships include:
Constant Value Description
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.
For example, suppose there were three existing nodes, and you wished to place a fourth node between the second and the third nodes, the code would be:
' Assuming the second node's Key value is "2 node".
tvwMyTree.Nodes.Add "2 node", tvwNext
Other arguments of the Add method are key, text, and image. Using these arguments, you can assign the Key, Text, and Image properties as the Node object is created.
For More Information For more information about the Nodes collection's Add method See "Add Method" by typing "Add Method" in the Index search and clicking "Add Method (Nodes Collection)."
A second way of adding nodes is to declare an object variable of type Node, and then use the Set statement with the Add method. The Set statement sets the object variable to the new node. You can then use the object variable to set the node's properties, as shown below:
Dim nodX As Node
Set nodX = tvwMyTree.Nodes.Add("10 node", tvwChild)
nodX.Key = "11 node"
nodX.Text = "IEEE"
nodX.Image = "closed"
Tip Using the Set statement with the Add method makes reading and debugging your code easier. However, using the Add method and its arguments to add nodes creates faster code.
xxx as node) 其形式为: object_nodeclick()