当列表控件选中当前Node时,来刷新子项与父项状态
Private Sub tvwMenuList_AfterCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs)
CheckNode(e.Node)
End Sub
Private Sub CheckNode(ByRef node As TreeNode)
If Not Checking Then
Checking = True
Dim CheckedNode As TreeNode = node
Dim checked As Boolean = CheckedNode.Checked
If checked Then
If CheckedNode.Nodes.Count > 0 Then
ChildNodeCheck(CheckedNode, checked)
'CheckedNode.BackColor = Color.White
End If
If CheckedNode.FullPath.ToString.Split("\").Length > 1 Then
CheckParentNode(CheckedNode.Parent, checked)
End If
Checking = False
Else
If CheckedNode.Nodes.Count > 0 Then
ChildNodeCheck(CheckedNode, checked)
'CheckedNode.BackColor = Color.White
End If
If CheckedNode.FullPath.ToString.Split("\").Length > 1 Then
CheckParentNode(CheckedNode.Parent, checked)
End If
Checking = False
End If
End If
End Sub
Private Sub ChildNodeCheck(ByVal node As TreeNode, ByVal checked As Boolean)
Dim _node As TreeNode
For Each _node In node.Nodes
_node.Checked = checked
'_node.BackColor = Color.White
If _node.Nodes.Count > 0 Then
ChildNodeCheck(_node, checked)
End If
Next
End Sub
Private Sub CheckParentNode(ByVal node As TreeNode, ByVal checked As Boolean)
Dim _node As TreeNode
Dim _full, _empty As Integer
For Each _node In node.Nodes
If _node.Checked Then
_full = _full + 1
Else
_empty = _empty + 1
End If
Next
If checked Then
If _full = node.Nodes.Count Then
node.Checked = checked
'node.BackColor = Color.White
If node.FullPath.ToString.Split("\").Length > 1 Then
CheckParentNode(node.Parent, checked)
End If
Exit Sub
Else
node.Checked = checked
'node.BackColor = Color.Red
If node.FullPath.ToString.Split("\").Length > 1 Then
ParentNodeCheck(node.Parent, checked)
End If
Exit Sub
End If
Else
If _empty = node.Nodes.Count Then
node.Checked = checked
'node.BackColor = Color.White
If node.FullPath.ToString.Split("\").Length > 1 Then
CheckParentNode(node.Parent, checked)
End If
Exit Sub
Else
node.Checked = Not checked
'node.BackColor = Color.Red
If node.FullPath.ToString.Split("\").Length > 1 Then
ParentNodeCheck(node.Parent, Not checked)
End If
Exit Sub
End If
End If
End Sub
Private Sub ParentNodeCheck(ByVal node As TreeNode, ByVal checked As Boolean)
node.Checked = checked
'node.BackColor = Color.Red
If node.FullPath.ToString.Split("\").Length > 1 Then
ParentNodeCheck(node.Parent, checked)
End If
End Sub