请问TreeView的用法

youk 2000-02-25 10:50:00
首先感谢Lin先生提供的TreeView Active 控件
但是在VB的帮助里没有找到TreeView的详细说明,一般的参考书里也没有详述.
能否提供一个程序说明怎样用TreeView实现象Explorer左边目录框中的全般表示,
或者告诉我到哪里能找到这方面的源程序.
...全文
175 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
grep 2000-05-04
  • 打赏
  • 举报
回复
如何为网叶中使用treeview控件呢?好象跟在vb中不一样
比如nodeclick事件在javascript中不象vb中: object_nodeclick(byval
xxx as node) 其形式为: object_nodeclick()
如何处理其单击事件,找不到单击对象呀?
那为大侠可指点一二,先谢过了
ding 2000-02-27
  • 打赏
  • 举报
回复
如今在windows环境下写程序,一册MSDN是必不可少的。
cloud 2000-02-25
  • 打赏
  • 举报
回复
另外,在MSDN能查到TREEVIEW。以下便是摘自MSDN:
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



Possible Uses
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:

MsgBox tvwMyTree.Nodes(10).Children

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:

MsgBox tvwMyTree.Nodes(10).Parent.Text
MsgBox tvwMyTree.Nodes(10).Parent.Index

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.


--------------------------------------------------------------------------------
Send

用TreeView可查到。
cloud 2000-02-25
  • 打赏
  • 举报
回复
TreeView 中添加数据的方法可以参照以下代码:
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

Me.treCategory.Nodes.Clear
treCategory.ImageList = Me.imlCategory
Set xnode = Me.treCategory.Nodes.Add(, , "Category", "Category", "Root")
If colTableData Is Nothing Then
Exit Sub
End If

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"
End If
Next lnColCount

Set colTableData = Nothing

bOperatSuccess = True
Exit Sub

至于风格问题,可以在属性里面设置。
Davy 2000-02-25
  • 打赏
  • 举报
回复
到湖南信息港看看,那有一本控件参考手册

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧