Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSelect a TreeView subtree when the user selects a node in VB .NET
Description
KeywordsVB.NET, TreeView, subtree, select
CategoriesControls, VB.NET
 
In the TreeView's AfterCheck event handler, see if the clicked node is checked or unchecked and call subroutine SetSubtreeChecked. SetSubtreeChecked sets a node's Checked value and then recursively calls itself to set the Checked value for that node's descendants.
 
' Check or uncheck all nodes in this node's subtree.
Private Sub trvMeals_AfterCheck(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.TreeViewEventArgs) _
    Handles trvMeals.AfterCheck
    Dim parent_node As TreeNode = e.Node
    Dim is_checked As Boolean = parent_node.Checked
    For i As Integer = 0 To e.Node.Nodes.Count - 1
        SetSubtreeChecked(parent_node.Nodes(i), is_checked)
    Next i
End Sub

' Set the Checked property for all nodes
' in the subtree.
Private Sub SetSubtreeChecked(ByVal parent_node As _
    TreeNode, ByVal is_checked As Boolean)
    ' Set the parent's Checked value.
    parent_node.Checked = is_checked

    ' Set the child nodes' Checked values.
    For i As Integer = 0 To parent_node.Nodes.Count - 1
        SetSubtreeChecked(parent_node.Nodes(i), is_checked)
    Next i
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated