Results 1 to 15 of 15

Thread: Treeview - Check all Child Nodes on checking parent node

  1. #1

    Thread Starter
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Treeview - Check all Child Nodes on checking parent node

    I was wondering how I would go about placing a check in the checkboxes for each child node of a parent node in a TreeView control, if I was to check the parent node.

    For example:

    • Parent 1
      • Child 1
      • Child 2
      • Child 3


    If I was to check "Parent 1" how would I be able to make that event place a checkbox beside all of the child nodes of "Parent 1"? (Child1, 2, and 3.)
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Treeview - Check all Child Nodes on checking parent node

    Handle the AfterCheck event of the TreeView. In the event handler, get the node, test whether it's checked or not and, if it is, loop through its Nodes collection and check each item. That will recurse of its own accord.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Treeview - Check all Child Nodes on checking parent node

    Actually, I take back what I said about that recursing of its own accord. I just read the documentation and the event is only raised if the node is checked through the UI.

    The documentation for the AfterCheck event actually has a code example that does exactly what you want to do. As such, you should have been able to answer this question for yourself. You knew that you wanted to do something with the TreeView control so the first thing you should have done is read the documentation for the TreeView control. In doing so, you would have seen that there was an AfterCheck event that was raised after a node is checked so you would have followed the link to the documentation for that event. You then would have seen that code example. Always read the documentation first and you will find many, if not most, of your questions answered.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Treeview - Check all Child Nodes on checking parent node

    I already know about the AfterCheck event, I have handles on it right now actually in my code. I'm just not sure how I would loop through the sub/child nodes of a specific parent node, and check them all, or uncheck them all based on the checkstate of the parent.

    Where is this documentation btw?

    I have lots of different "depths" or "levels" inside my TreeView control, but i've never used a lot of the control in the past. How would you define specific "levels" based on the treeview expansion areas?
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Treeview - Check all Child Nodes on checking parent node

    The documentation is the MSDN Library, which you should have installed with VS or VB Express and then access via the Help menu, where every Windows application makes its documentation available. If you haven't installed it then I strongly suggest that you do. If you don't want to for some reason, you can still access it online.

    A tree is an inherently recursive structure and the code example I mentioned uses recursion to go from one parent to all its children and all their children and so on.

    There is no specific differentiation amongst nodes in a TreeView. If you want to differentiate then you can write the code to do so based on the Index and/or Level properties of the nodes.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Treeview - Check all Child Nodes on checking parent node

    Hmm, thanks for all the info, I personally don't use the MSDN Library that often, and i've never accessed it through the help menu in Visual Basic. Only online a couple of times, I've usually figured out most of what I know by trial and error, and looking at how others go about using a code to a certain advantage.

    As a result, I think I understand the code a lot more than someone who just visits MSDN to copy and paste what they see. But I think I need to look at how recursion works with the TreeView control. It's still fresh for me, as I'm twice as familiar with other controls like listboxes, listviews, checklistboxes, etc.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  7. #7

    Thread Starter
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Treeview - Check all Child Nodes on checking parent node

    Ohh, one more thing I was wondering. Here is my application. I want to be able to group nodes together based on their keys. How would I combine the child nodes into a single parent node so that no duplicates are visible?

    MD5 hashes of the file are present for a node, but I have duplicates of those MD5 value keys, and I want to combine all of the child nodes with the same MD5 key into only one node.

    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Treeview - Check all Child Nodes on checking parent node

    This is some freehand typed code, so might contain errors. But it should give you an idea how to go about it.
    Code:
    Sub CheckChildNodes(byval parent as TreeNode, checked as Boolean)
        For each child as TreeNode in parent.Nodes
            child.Checked=checked
            If child.Nodes.Count > 0 Then CheckChildNodes(child, checked)
        Next
    End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Treeview - Check all Child Nodes on checking parent node

    Quote Originally Posted by AceInfinity View Post
    As a result, I think I understand the code a lot more than someone who just visits MSDN to copy and paste what they see.
    I certainly wasn't suggesting that you, or anyone, should just go to MSDN to copy and paste. You must not have seen to many of my posts if you think that. I'm all about reading the documentation to get the hows and whys of each type and member so that you get a proper understanding. Trial and error has its place but it's a big waste of time if there's a clear explanation available in the documentation, as there is on this occasion.

    EVERY time I have a question when I already know what type or member I need to use, I visit the MSDN Library first. I generally do even if I don't know what type or member I need, unless experience tells me that I'm likely to find the answer quicker elsewhere. That's what I've done the whole of my professional career and it's one of the primary reasons that I have been able to be a Microsoft MVP for the last five years. There's no reason that everyone else shouldn't do the same thing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Treeview - Check all Child Nodes on checking parent node

    I can't seem to get the code to work

    I have:
    Code:
    Sub CheckChildNodes(ByVal Parent As TreeNode, ByVal Checked As Boolean)
            For Each child As TreeNode In Parent.Nodes
                child.Checked = Checked
                If child.Nodes.Count > 0 Then
                    CheckChildNodes(child, Checked)
                End If
            Next
        End Sub
    However i'm calling it with

    Code:
    CheckChildNodes(ParentNodeHash, True)
    And no change in the checks seems to happen. It's in the code with the handle on TreeView1.AfterCheck
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Treeview - Check all Child Nodes on checking parent node

    This is the code from the MSDN documentation:
    vb.net Code:
    1. ' Updates all child tree nodes recursively.
    2. Private Sub CheckAllChildNodes(treeNode As TreeNode, nodeChecked As Boolean)
    3.    Dim node As TreeNode
    4.    For Each node In  treeNode.Nodes
    5.       node.Checked = nodeChecked
    6.       If node.Nodes.Count > 0 Then
    7.          ' If the current node has child nodes, call the CheckAllChildsNodes method recursively.
    8.          Me.CheckAllChildNodes(node, nodeChecked)
    9.       End If
    10.    Next node
    11. End Sub
    12.  
    13. ' NOTE   This code can be added to the BeforeCheck event handler instead of the AfterCheck event.
    14. ' After a tree node's Checked property is changed, all its child nodes are updated to the same value.
    15. Private Sub node_AfterCheck(sender As Object, e As TreeViewEventArgs) Handles treeView1.AfterCheck
    16.    ' The code only executes if the user caused the checked state to change.
    17.    If e.Action <> TreeViewAction.Unknown Then
    18.       If e.Node.Nodes.Count > 0 Then
    19.          ' Calls the CheckAllChildNodes method, passing in the current
    20.          ' Checked value of the TreeNode whose checked state changed.
    21.          Me.CheckAllChildNodes(e.Node, e.Node.Checked)
    22.       End If
    23.    End If
    24. End Sub
    It works perfectly as is. If you want to just check children and not uncheck them when their parent is unchecked, it's a small change:
    Code:
    ' Updates all child tree nodes recursively.
    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
    
    ' NOTE   This code can be added to the BeforeCheck event handler instead of the AfterCheck event.
    ' After a tree node's Checked property is changed, all its child nodes are updated to the same value.
    Private Sub node_AfterCheck(sender As Object, e As TreeViewEventArgs) Handles treeView1.AfterCheck
       ' The code only executes if the user caused the checked state to change.
       If e.Action <> TreeViewAction.Unknown AndAlso e.Node.Checked Then 
          If e.Node.Nodes.Count > 0 Then
             ' Calls the CheckAllChildNodes method, passing in the current 
             ' Checked value of the TreeNode whose checked state changed. 
             Me.CheckAllChildNodes(e.Node, e.Node.Checked)
          End If
       End If
    End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Treeview - Check all Child Nodes on checking parent node

    That seemed to work, thanks, I did have to make a few modifications though, but I finally got it to work.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  13. #13
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Treeview - Check all Child Nodes on checking parent node

    Hello AceInfinity,

    Your new question was split into it's own thread here:

    http://www.vbforums.com/showthread.php?t=654522

    Gary

  14. #14

    Thread Starter
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Treeview - Check all Child Nodes on checking parent node

    Alright, sorry about that, thankyou for taking the time to edit my thread
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  15. #15
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Treeview - Check all Child Nodes on checking parent node

    Quote Originally Posted by AceInfinity View Post
    Alright, sorry about that, thankyou for taking the time to edit my thread
    Not a problem, we are here to help

    Gary

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