- TreeView Control
TreeView control is a powerfull tool to display the hierarchical data. We can use its CheckBoxes property to display the CheckBoxes against each node and user is allowed to select/de-select the nodes.
Sometimes it is required to get the nodes which are checked. For this purpose we can iterate through Nodes using Recursion but this process sometimes makes significant delay if there are many items in the Tree.
Another way of getting the selected/checked nodes is that
|We create a List of Nodes and each time when user clicks on a node we check if user has Checked the node then we add it in our list and if he/she has unchecked the node then we remove it from our list. And finally we will use this List of Nodes to see which one is selected/checked|
Following is the Form1 Code. I have put a TreeView and a Button Control on this form to test:
Dim CheckedNodes As List(Of TreeNode)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CheckedNodes = New List(Of TreeNode)
Private Sub TreeView1_BeforeCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeCheck
' if NOT checked and going to be checked
If Not e.Node.Checked Then
Else ' else (if checked and going to be un-chekced)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(Me.CheckedNodes.Count & " Nodes are selected", MsgBoxStyle.Information, "Form1")