Treeview检查并取消选中

时间:2010-03-03 10:35:30

标签: vb.net

 Private Function Check(ByVal mytreeNode As TreeNodeCollection) As Boolean
        For Each node As TreeNode In mytreeNode
            If node.ChildNodes.Count > 0 Then
              If node.Checked = True Then
                For Each chknode As TreeNode In node.ChildNodes
                    chknode.Checked = True
                Next
            End If
          Check(node.ChildNodes)
        Next
    End Function

使用此功能,我可以检查父节点时检查子节点。我想反过来。一旦我取消选中父节点也取消选中。可以使用同样的功能。?

2 个答案:

答案 0 :(得分:0)

你的意思是把它改成使用传入的参数。像

这样的东西
Private Function Check(ByVal mytreeNode As TreeNodeCollection, checked as Boolean) As Boolean 
    For Each node As TreeNode In mytreeNode 
        If node.ChildNodes.Count > 0 Then 
          If node.Checked = checked Then 
            For Each chknode As TreeNode In node.ChildNodes 
                chknode.Checked = checked 
            Next 
        End If 
      Check(node.ChildNodes, checked) 
    Next 
End Function 

答案 1 :(得分:0)

再添加一个参数

Private Function Check(ByVal mytreeNode As TreeNodeCollection,IsparentNodeChecked as boolean) As Boolean
        For Each node As TreeNode In mytreeNode
            If node.ChildNodes.Count > 0 Then
                For Each chknode As TreeNode In node.ChildNodes
                    chknode.Checked = IsparentNodeChecked 
                Next
            End If
          Check(node.ChildNodes,node.checked)
        Next
    End Function

试试这个