對于treeview的節(jié)點訪問我只發(fā)現(xiàn)了使用nodes屬性,但是Nodes.Item(index)屬性需要取得指定節(jié)點的index,在經(jīng)過一系列的增加和刪除操作以后,這個index已經(jīng)不連續(xù)了,我如果才能遍歷所有的節(jié)點呢?
treeview的node的幾個屬性可以幫助遍歷樹
1、parent '節(jié)點的父節(jié)點
2、child '節(jié)點的子節(jié)點
3、children 節(jié)點的子節(jié)點的個數(shù)
4、FirstSibling 屬性 這個屬性返回對 TreeView 控件中的 Node 對象的第一個相鄰對象的引用。
5、LastSibling 屬性 這個屬性返回對 TreeView 控件中的 Node 對象的最后相鄰節(jié)點的引用。
示例:
Private Sub TreeView1_NodeClick(ByVal Node As Node)
Dim strText As String
Dim n As Integer
'設(shè)置 n 為第一個相鄰節(jié)點的索引。
n = Node.FirstSibling.Index
'在字符串變量中安放第一個相鄰節(jié)點的文本和換行。
strText = Node.FirstSibling.Text & vbLF
While n <> Node.LastSibling.Index
'當 n 不是最后相鄰節(jié)點的索引時,轉(zhuǎn)向下一個相鄰的節(jié)點
'并將它的文本安放到字符串變量中。
strText = strText & TreeView1.Nodes(n).Next.Text & vbLF
'設(shè)置 n 為下一個節(jié)點的索引。
n = TreeView1.Nodes(n).Next.Index
Wend
MsgBox strText '顯示結(jié)果。
End Sub
另一種方法:
Do While Fn <= .Nodes.Count
'獲得信息
If .Nodes(NodeKey).Children <> 0 Then
NodeKey = .Nodes(NodeKey).Child.Key
ElseIf .Nodes(NodeKey).Next Is Nothing = False Then
NodeKey = .Nodes(NodeKey).Next.Key
Else
Do While .Nodes(NodeKey).Parent.Next Is Nothing
If .Nodes(NodeKey).Parent Is Nothing = False Then NodeKey = .Nodes(NodeKey).Parent.Key
Loop
Do While .Nodes(NodeKey).Parent.Next Is Nothing = False
NodeKey = .Nodes(NodeKey).Parent.Next.Key
Exit Do
Loop
End If
Fn = Fn + 1
Loop