Results 1 to 3 of 3

Thread: [RESOLVED] Checkbox not work properly when clicked on quickly multiple times

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2020
    Posts
    4

    Resolved [RESOLVED] Checkbox not work properly when clicked on quickly multiple times

    I have done some code for checking treeview node
    Code:
         Private Sub TreeView1_AfterCheck(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterCheck
            If updatingTreeView Then Return
            updatingTreeView = True
            CheckNode(e.Node, e.Node.Checked)
            HasCheckedChildNode = 0
            updatingTreeView = False
        End Sub
    
        Private Sub CheckNode(node As TreeNode, isChecked As Boolean)
            If node.Parent IsNot Nothing Then
                HasCheckedNode(node.Parent)
                If Not isChecked And HasCheckedChildNode > 0 Then Return
                node.Parent.Checked = isChecked
            ElseIf node.Parent Is Nothing Then
                For Each cn As TreeNode In node.Nodes
                    cn.Checked = isChecked
                Next
            End If
        End Sub
    
        Private Sub HasCheckedNode(node As TreeNode)
            For Each cn As TreeNode In node.Nodes
                If cn.Checked = True Then
                    HasCheckedChildNode += 1
                ElseIf cn.Checked = False Then
                    HasCheckedChildNode -= 0
                End If
            Next
        End Sub
    what this code do:
    1. If parent node is checked/unchecked, also check/uncheck all child nodes
    2. If just one child node is checked, also check parent node
    3. If the last child node is unchecked, also uncheck parent node

    this code works fine.

    Problem:
    When I clicks quickly some of the checkboxes are checked and some no
    E.g. sometimes I checked the parent node but all child nodes still remain uncheck
    Name:  4-16-2020-1-46-19-PM.gif
Views: 433
Size:  65.4 KB

    Is there a reason for this?
    Please let me know if I missed anything.

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

    Re: Checkbox not work properly when clicked on quickly multiple times

    try it this way
    Code:
     Private Sub TreeView1_AfterCheck(sender As Object, e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCheck
            With e.Node
                For Each nd As TreeNode In .Nodes
                    nd.Checked = .Checked
                Next
            End With
        End Sub
    hth
    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2020
    Posts
    4

    Re: Checkbox not work properly when clicked on quickly multiple times

    This problem has been solved.
    Please visit:
    this link

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