Private Sub tvLevel_NodeClick(ByVal Node As MSComctlLib.Node)
'/*如果當(dāng)前為未選中或模糊狀態(tài),則變?yōu)檫x中,否則為變?yōu)槲催x中
'/*如果變?yōu)檫x中狀態(tài),則所有子節(jié)點變?yōu)檫x中
'/*如果變?yōu)槲催x中狀態(tài),則所有子節(jié)點變?yōu)槲催x中
'/*父節(jié)點根據(jù)兄弟節(jié)點而定
If Node.Image = "selall" Then
Node.Image = "selno"
Call chgChild(Node, False)
Else
Node.Image = "selall"
Call chgChild(Node, True)
End If
Call chgParent(Node)
End Sub
Private Sub chgChild(mNode As MSComctlLib.Node, selected As Boolean)
'/*改變所有子節(jié)點狀態(tài)
Dim nodX As Node
Dim nodTmp As Node
If mNode.Child Is Nothing Then Exit Sub
Set nodX = mNode.Child
Do While Not nodX.Key = mNode.Key
If selected Then
nodX.Image = "selall"
Else
nodX.Image = "selno"
End If
If Not nodX.Child Is Nothing Then '/*有子節(jié)點,指向子節(jié)點
Set nodX = nodX.Child
Else '/*如果沒有子節(jié)點,則指向下一個兄弟節(jié)點
If Not nodX.Next Is Nothing Then '/*如果有下一個兄弟節(jié)點,則指向下一個兄弟節(jié)點
Set nodX = nodX.Next
Else '/*兄弟已盡,指向父節(jié)點,然后指向下一個兄弟節(jié)點
Do While (nodX.Next Is Nothing And nodX.Key <> mNode.Key)
Set nodX = nodX.Parent
Loop
End If
End If
Loop
End Sub
Private Sub chgParent(mNode As MSComctlLib.Node)
'/*改變父級節(jié)點的狀態(tài)
Dim count As Integer '/*標(biāo)志
Dim nodX As Node
Dim nodTmp As Node
Dim i As Integer
Set nodX = mNode.Parent
Do While Not nodX Is Nothing
'/*判斷一個節(jié)點狀態(tài)
count = 0
Set nodTmp = nodX.Child
For i = 1 To nodX.Children
If nodTmp.Image = "selall" Then
count = count + 2
End If
If nodTmp.Image = "selsome" Then
count = count + 1
End If
If i < nodX.Children Then
Set nodTmp = nodTmp.Next
End If
Next i
nodX.Image = "selsome"
If count = 2 * nodX.Children Then '/*全為選中狀態(tài)
nodX.Image = "selall"
End If
If count = 0 Then '/*全為未選中狀態(tài)
nodX.Image = "selno"
End If
Set nodX = nodX.Parent
Loop
End Sub
treeview不使用checkbox,因為它只有兩種狀態(tài)
如果您有更好的方法,請寫在下面:)