开发者

Checking Treeview nodes

I'm all new to this treeview business. I have a fixed treeview, very simple and basic. Only two levels, parent and child nodes. I just need two things out of this treeview.

1) If parent is checked, check all child nodes.

2) If just one child node is unchecked, uncheck parent node.

That's it! I've bee开发者_如何学JAVAn looking for this for 30 mins and I cant find it. Maybe because my vb.net is old, it doesnt seem to be the same as others =/ I'm using vb.net 2003. Thank you very much :D


Working example in VB.Net 2008, but it should work in 2003:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
  For i As Integer = 1 To 3
    Dim node As New TreeNode("Parent #" & i.ToString)
    node.Nodes.Add("Child #1")
    node.Nodes.Add("Child #2")
    node.Expand()

    TreeView1.Nodes.Add(node)
  Next
End Sub

Private Sub TreeView1_AfterCheck(ByVal sender As Object, ByVal e As TreeViewEventArgs) Handles TreeView1.AfterCheck
  RemoveHandler TreeView1.AfterCheck, AddressOf TreeView1_AfterCheck

  For Each node As TreeNode In e.Node.Nodes
    node.Checked = e.Node.Checked
  Next

  If e.Node.Checked Then
    If e.Node.Parent Is Nothing = False Then
      Dim allChecked As Boolean = True

      For Each node As TreeNode In e.Node.Parent.Nodes
        If Not node.Checked Then
          allChecked = False
        End If
      Next

      If allChecked Then
        e.Node.Parent.Checked = True
      End If

    End If
  Else
    If e.Node.Parent Is Nothing = False Then
      e.Node.Parent.Checked = False
    End If
  End If

  AddHandler TreeView1.AfterCheck, AddressOf TreeView1_AfterCheck
End Sub


You may want to try this shorter version

   Private Sub treeview1_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles treeview1.AfterCheck

    Dim objNode As TreeNode = e.Node
    Dim bCheck As Boolean = e.Node.Checked

    For Each ChildNode As TreeNode In objNode.Nodes
        ChildNode.Checked = bCheck
    Next
End Sub


I had basically the same requirement, but I also wanted that if all child nodes are checked, check the parent node.

Here is my code tested today in VS 2013:

Private Sub TreeView1_AfterCheck(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterCheck
    If e.Action <> TreeViewAction.ByMouse And e.Action <> TreeViewAction.ByKeyboard Then Exit Sub

    Dim CheckedNode As TreeNode = e.Node
    Dim CheckedNodeIsRoot As Boolean = CheckedNode.Parent Is Nothing
    Dim ParentNode As TreeNode = CheckedNode.Parent

    Dim ChildOneUnchecked As Boolean = False

    If CheckedNodeIsRoot Then
        For Each ChildNode As TreeNode In CheckedNode.Nodes
            ChildNode.Checked = CheckedNode.Checked
        Next
    Else
        If CheckedNode.Checked Then
            For Each ChildNode As TreeNode In ParentNode.Nodes
                If Not ChildNode.Checked Then ChildOneUnchecked = True
            Next
            If ChildOneUnchecked Then
                ParentNode.Checked = False
            Else
                ParentNode.Checked = True
            End If
        Else
            ParentNode.Checked = False
        End If
    End If
End Sub


This works on VB.net 2010. it is based on Daniel Lee code

Private Sub TreeView1_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCheck

    Dim childNodeCK As TreeNode = e.Node
    Dim NodeChecked As Boolean = e.Node.Checked

    For Each ChildNode As TreeNode In childNodeCK.Nodes
        If NodeChecked = True Then
            ChildNode.Checked = NodeChecked
        End If
    Next

    If childNodeCK.Checked = False Then
        If e.Node.Parent Is Nothing = False Then
            e.Node.Parent.Checked = False
        End If
    End If

End Sub


Private Sub Tv1_AfterCheck(sender As Object, e As TreeViewEventArgs) Handles Tv1.AfterCheck
    Try
        Dim A As Boolean = e.Node.Checked
        If e.Action = TreeViewAction.ByMouse Or e.Action = TreeViewAction.ByKeyboard Then
            CUchkall(e.Node, A)
        End If
        UnCheckParentNodes(e.Node)
        Call CuChild(e.Node)
    Catch
    End Try
End Sub

Private Sub CuChild(ByVal iNode As TreeNode)
    Try
        Dim E As Boolean = True
        If iNode.Parent IsNot Nothing Then
            For Each M As TreeNode In iNode.Parent.Nodes
                If M.Checked = False Then E = False
            Next
            iNode.Parent.Checked = E
        End If
    Catch 
    End Try
End Sub

Private Sub CUchkall(ByVal iNode As TreeNode, COU As Boolean)
    Try
        If iNode.Nodes IsNot Nothing Then
            For Each N As TreeNode In iNode.Nodes
                N.Checked = COU
                Call CUchkall(N, COU)
            Next
        End If
    Catch
    End Try
End Sub

Private Sub UnCheckParentNodes(ByVal iNode As TreeNode)
    Try
        If iNode.Checked = False AndAlso iNode.Parent IsNot Nothing Then
            iNode.Parent.Checked = False
            UnCheckParentNodes(iNode.Parent)
        End If
    Catch
    End Try
End Sub


I modify Javier Arce codes for multilevel treeview (recursive version). Here is my codes.

Private Sub treeTestsSelection_AfterCheck(sender As Object, e As TreeViewEventArgs) Handles treeTestsSelection.AfterCheck
        If e.Action <> TreeViewAction.ByMouse And e.Action <> TreeViewAction.ByKeyboard Then Exit Sub

        CheckAllNodes(e.Node)
        CheckAllNodesForParent(e.Node)
End Sub

Public Sub CheckAllNodes(ByRef TreeNodeToCheck As TreeNode)
    Dim val As Boolean = TreeNodeToCheck.Checked
    For Each n As TreeNode In TreeNodeToCheck.Nodes
        n.Checked = val
        If n.GetNodeCount(True) > 0 Then
            CheckAllNodes(n)
        End If
    Next
End Sub

Public Sub CheckAllNodesForParent(ByRef CheckedTreeNode As TreeNode)
    If CheckedTreeNode.Checked Then
        If CheckedTreeNode.Parent Is Nothing = False Then
            Dim allChecked As Boolean = True
            For Each node As TreeNode In CheckedTreeNode.Parent.Nodes
                If Not node.Checked Then
                    allChecked = False
                End If
            Next
            If allChecked Then
                CheckedTreeNode.Parent.Checked = True
                CheckAllNodesForParent(CheckedTreeNode.Parent)
            End If
        End If
    Else
        If CheckedTreeNode.Parent Is Nothing = False Then
            CheckedTreeNode.Parent.Checked = False
            CheckAllNodesForParent(CheckedTreeNode.Parent)
        End If
    End If
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜