给你的源码是让所有的node同步
Private Sub setchildrennodes(node As node)
If node Is Nothing Then Exit Sub
If issystemcontrol = True Then Exit Sub
Dim child As node
Set child = node.child
If child Is Nothing Then Exit Sub
Dim nodecounter As Integer
If node.Children = 0 Then Exit Sub
For nodecounter = 1 To node.Children
issystemcontrol = True
child.Checked = node.Checked
issystemcontrol = False
Set child = child.Next
Call setchildrennodes(child)
Next
End Sub
Private Sub setparentnodes(node As node)
If node Is Nothing Then Exit Sub
Dim parentnode As node
Dim nownode As node
Dim flag As Boolean
Dim counter As Integer
flag = True
Set parentnode = node.Parent
If parentnode Is Nothing Then Exit Sub
Set nownode = node.FirstSibling
For counter = 1 To parentnode.Children
flag = flag And nownode.Checked
Set nownode = nownode.Next
Next
issystemcontrol = True
parentnode.Checked = flag
issystemcontrol = False
Call setparentnodes(parentnode)
End Sub
dim childNode as Node
dim thisIndex as long
thisIndex=当前选中的(或取消的)Node.index
for each childNode in TreeView1.nodes
if chileNode.Parent.index=thisIndex then
chileNode.Checked=True '(or False)
end if
next
大概如此,你试试看吧。
递归一下嘛,小猴子,给你一段我的代码参考一下:
Private Function DelNode(mNode As node) As Boolean
'删除一个节点和此节点下的所有节点
Dim nodX As node
Dim nodTmp As node
'/*如果当前节点下无任何子节点,则一步到位地删除
If mNode Is Nothing Then Exit Function
If mNode.Key = "N0" Then Exit Function
If MsgBox("是否确认删除此节点?", vbInformation + vbOKCancel, "提示") = vbCancel Then
Exit Function
End If
If mNode.Child Is Nothing Then
Call DelSngNode(mNode)
Exit Function
End If
Set nodX = mNode.Child
Do While Not nodX.Key = (mNode.Key)
If nodX.Child Is Nothing Then
Set nodTmp = nodX.Parent
Call DelSngNode(nodX)
Set nodX = nodTmp
Else
Set nodX = nodX.Child
End If
Loop
Call DelSngNode(nodX)
End Function
Private Function DelSngNode(mNode As node) As Boolean
'仅删除一个节点,供DelNOde调用
Dim l As Long
'/*数据库中删除此条记录
l = CLng(Right(mNode.Key, Len(mNode.Key) - 1))
ac.Execute "delete from data where num=" & l
'/*删除此节点
tvFile.Nodes.Remove (mNode.Key)
End Function