Results 1 to 8 of 8

Thread: [RESOLVED] uncheck all parent nodes

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2021
    Posts
    71

    Resolved [RESOLVED] uncheck all parent nodes

    I have a Treeview with some nodes.

    Code:
    TreeView1.CheckBoxes = True
    TreeView1.Nodes.Add("Archive")
            YearsPath.ForEach(Sub(wf)
                                  TreeView1.Nodes(0).Nodes.Add(wf.Year)
                              End Sub)
            TreeView1.ExpandAll()
    I am looking for a way to uncheck all items when I click a button, so not with the Treeview aftercheck
    Last edited by clausowitz; Apr 21st, 2021 at 05:37 PM. Reason: changed my mind

  2. #2
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: uncheck all parent nodes

    Quote Originally Posted by clausowitz View Post
    I have a Treeview with some nodes.
    I am looking for a way to uncheck all items when I click a button, so not with the Treeview aftercheck
    You would use the same code that performs the node unchecking, except you would call it from a Button.Click event handler rather than from the TreeView.AfterCheck event handler.

  3. #3
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,045

    Re: uncheck all parent nodes

    to clear all checked nodes, somthing like this

    Code:
     Private Sub uncheckTree(ByVal aTreeView As TreeView)
            Dim n As TreeNode
            For Each n In aTreeView.Nodes
                n.Checked = False
            Next
        End Sub
    
    'and call like this
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            uncheckTree(TreeView1)
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2021
    Posts
    71

    Re: uncheck all parent nodes

    Chris, the code works however what I needed was a way to change all StateImageIndex so the flag is replaced with a dotted line image. That way it shows no items were selected.

    Attachment 181103

  5. #5
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: uncheck all parent nodes

    Quote Originally Posted by clausowitz View Post
    Chris, the code works however what I needed was a way to change all StateImageIndex so the flag is replaced with a dotted line image. That way it shows no items were selected.
    Not sure what you're doing - the attachment isn't showing.

    Do you have the TreeView.CheckBoxes property set to True or False?

    If it is set to True and you also have a TreeView.StateImageList set up with the two images you want to toggle between at indexes 0 and 1, then the two images will be used instead of CheckBoxes; one image representing checked and the other representing unchecked. So Chris's code will work without modification. See the Remarks Section of the documentation for the TreeNode.StateImageIndex Property:
    If the parent TreeView has check boxes enabled, the StateImageIndex is ignored and the node will display the first or second image in the StateImageList set on the parent TreeView to indicate an unchecked or checked state, respectively.
    If the TreeView.CheckBoxes property set to False, then I imagine you'll still use the same approach as ChrisE showed (possibly with a bit of recursion thrown in to clear all the child nodes if required), only you'll need to set the TreeNode.StateImageIndex for each TreeNode to the required value instead of setting the node's Checked property.
    Last edited by Inferrd; Apr 22nd, 2021 at 06:20 AM.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2021
    Posts
    71

    Re: uncheck all parent nodes

    The TreeView.CheckBoxes property is set to True. TreeView.StateImageList is set up with the two images. Index 0 is a dotted line to make it appear empty. Index 1 is a flag.
    Still the images don't change when I run Eric's code because it only unchecks the node I use. The images are used in children.
    Last edited by clausowitz; Apr 22nd, 2021 at 06:51 AM.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2021
    Posts
    71

    Re: uncheck all parent nodes

    I did some more research and got it to work using this code:

    Code:
    Private Sub UncheckTree(ByVal aTreeView As TreeView)
            Dim n As TreeNode
            For Each n In aTreeView.Nodes
                'n.Checked = False
                CheckAllChildNodes(n, False)
            Next
        End Sub
        Private Sub CheckAllChildNodes(treeNode As TreeNode, nodeChecked As Boolean)
            Dim node As TreeNode
            For Each node In treeNode.Nodes
                node.Checked = nodeChecked
                If node.Nodes.Count > 0 Then
                    ' If the current node has child nodes, call the CheckAllChildsNodes method recursively.
                    Me.CheckAllChildNodes(node, nodeChecked)
                End If
            Next node
        End Sub

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: uncheck all parent nodes

    Slightly different approach

    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'test
            Static chk As Boolean = False
            chk = Not chk
            SetAllCheckState(chk, TreeView1)
        End Sub
    
        Public Sub SetAllCheckState(state As Boolean, whTV As TreeView)
            Dim ltn As List(Of TreeNode) = AllTreeNodes(whTV).ToList
            For Each tn As TreeNode In ltn
                tn.Checked = state
            Next
        End Sub
    
        Private Iterator Function AllTreeNodes(whTV As TreeView) As IEnumerable(Of TreeNode)
            Dim myEnumerator As IEnumerator = whTV.Nodes.GetEnumerator()
            While myEnumerator.MoveNext()
                For Each stn As TreeNode In ChildrenTN(DirectCast(myEnumerator.Current, TreeNode))
                    Yield stn
                Next
            End While
        End Function
    
        Private Function ChildrenTN(ChildrenOf As TreeNode) As List(Of TreeNode)
            Dim rv As New List(Of TreeNode)
            rv.Add(ChildrenOf)
            For Each tn As TreeNode In ChildrenOf.Nodes
                rv.AddRange(ChildrenTN(tn))
            Next
            Return rv
        End Function
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width