vb的treeview控件如何实现三态的checkbox
我在treeview中设置了checkboxs
现在是这样的,我已经可以实现了:点击父节点,子节点全选和全不选。点击选中一个子节点,父节点也选中。
问题来了:
1、点击取消一个子节点,但是父节点仍然是选中状态
2、点击选中一个父节点后,所有的子节点都选中。但是,当所有的子节点都取消选中后,父节点仍然是选中状态
哪位大侠给我写个代码看看啊
=====以下是我自己写的代码======
Dim i As Integer '如果一个节点有子节点,当所有的子节点都没有选中,则该节点没有选中 If Node.Children <= 0 Then Dim parentNode As Node Dim tmpNode As Node Dim flg As Boolean Set parentNode = Node.Parent parentNode.Checked = True Set tmpNode = parentNode.FirstSibling flg = False For i = 1 To parentNode.Children If tmpNode.Checked = True Then flg = True Else flg = False End If Next 'parentNode.Checked = flg End If
Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node) Dim lChecked As Long lChecked = SetNodeChecked(Node, Node.Checked) If Not Node.Parent Is Nothing Then If (lChecked > 0) Then Node.Parent.Checked = True Else Node.Parent.Checked = GetNodeCheckedCount(Node.Parent) > 0 End If End IfEnd SubPrivate Function GetNodeCheckedCount(ByVal Node As MSComctlLib.Node) As Long Dim objNode As Node Dim lCount As Long If Not Node Is Nothing Then Set objNode = Node.Child Do While Not objNode Is Nothing If objNode.Checked Then lCount = lCount + 1 lCount = lCount + GetNodeCheckedCount(objNode) Set objNode = objNode.Next Loop End If GetNodeCheckedCount = lCountEnd FunctionPrivate Function SetNodeChecked(ByVal Node As MSComctlLib.Node, ByVal bChecked As Boolean) As Long Dim objNode As Node Dim lCount As Long If Not Node Is Nothing Then Set objNode = Node.Child Do While Not objNode Is Nothing objNode.Checked = bChecked If objNode.Checked Then lCount = lCount + 1 lCount = lCount + SetNodeChecked(objNode, bChecked) Set objNode = objNode.Next Loop End If SetNodeChecked = lCountEnd Function