TREEVIEW问题
怎样在TREEVIEW中实现单击父结点前checkbox则其下子结点checkbox全部被选择
[解决办法]
遍历父节点的所有子节点
[解决办法]
'**************************************
' Name: Auto Check/UnCheck TreeView
' Description:This basic procedure will
' handle all parent and child Checkboxes i
' n a TreeView control. If you check a chi
' ld node it will automatically check the
' parent(s) and vise versa.
' By: kc7dji
'
' Inputs:This procedure requires you to
' pass in the Treeview and the current nod
' e.
'
' Assumes:Basic TreeView(s). This code a
' ssumes you already have a TreeView popul
' ated with "CheckBoxes " turned ON.
'
'This code is copyrighted and has ' limited warranties.Please see http://w
' ww.Planet-Source-Code.com/vb/scripts/Sho
' wCode.asp?txtCodeId=30895&lngWId=1 'for details. '**************************************
'Put this call into the TreeViews NODECH
' ECK procedure.
Private Sub MyTreeView_NodeCheck(ByVal Node As MSComctlLib.Node)
Call TreeCheckBoxes(MyTreeView, Node)
End Sub
'Add this procedure to a Module or to th
' e form the TreeView is contained.
Public Sub TreeCheckBoxes(TR As TreeView, CurrentNode As Node)
'This code is copyright (c)2002 by Scott
' Durrett - All Rights Reserved
'No changes are allow without written ap
' proval from the Author.
Dim liNodeIndex As Integer
Dim lbDirty As Boolean
Dim liCounter As Integer
Dim lParentNode As Node
Dim lChildNode As Node
lbDirty = False
liNodeIndex = CurrentNode.Index
If CurrentNode.Checked = True Then 'node is checked
'Children Check/UnCheck
If Not TR.Nodes.Item(CurrentNode.Index).Child Is Nothing Then
Set lParentNode = TR.Nodes.Item(liNodeIndex).Child.FirstSibling
Do While Not lParentNode Is Nothing
lParentNode.Checked = CurrentNode.Checked
If Not lParentNode.Child Is Nothing Then
Set lChildNode = lParentNode.Child
Do While Not lChildNode Is Nothing
lChildNode.Checked = CurrentNode.Checked
If Not lChildNode.Next Is Nothing Then
Set lChildNode = lChildNode.Next
Else
Set lChildNode = lChildNode.Child
End If
Loop
End If
Set lParentNode = lParentNode.Next
Loop
End If
'=======================================
' =====================
'Check all parent nodes
Do While Not TR.Nodes.Item(liNodeIndex).Parent Is Nothing
TR.Nodes.Item(liNodeIndex).Parent.Checked = CurrentNode.Checked
liNodeIndex = TR.Nodes.Item(liNodeIndex).Parent.Index
Loop
'===========================
ElseIf CurrentNode.Checked = False Then 'node is unchecked
'Children Check/UnCheck
If Not TR.Nodes.Item(CurrentNode.Index).Child Is Nothing Then
Set lParentNode = TR.Nodes.Item(liNodeIndex).Child.FirstSibling
Do While Not lParentNode Is Nothing
lParentNode.Checked = CurrentNode.Checked
If Not lParentNode.Child Is Nothing Then
Set lChildNode = lParentNode.Child
Do While Not lChildNode Is Nothing
lChildNode.Checked = CurrentNode.Checked
If Not lChildNode.Next Is Nothing Then
Set lChildNode = lChildNode.Next
Else
Set lChildNode = lChildNode.Child
End If
Loop
End If
Set lParentNode = lParentNode.Next
Loop
End If
'=======================================
' =====================
Set lParentNode = Nothing
Set lChildNode = Nothing
If Not CurrentNode.Parent Is Nothing Then
Set lParentNode = CurrentNode.Parent.Child
Do While Not lParentNode Is Nothing
Set lChildNode = lParentNode.FirstSibling
Do While Not lChildNode Is Nothing
If lChildNode.Checked = True Then
lbDirty = True
Exit Do
End If
'If Not lChildNode.Next Is Nothing Then
Set lChildNode = lChildNode.Next
'End If
Loop
If lbDirty = False Then
If Not lParentNode.Parent Is Nothing Then
lParentNode.Parent.Checked = False
lbDirty = False
End If
Else
Exit Do
End If
If Not lParentNode.Parent Is Nothing Then
Set lParentNode = lParentNode.Parent
Else
Set lParentNode = lParentNode.Parent
End If
Loop
End If
End If
Set CurrentNode = Nothing
Set lParentNode = Nothing
Set lChildNode = Nothing
End Sub