Friday, September 19, 2014

Find Checked Treenode in TreeView control using LINQ

Here's one way of searching through a treenode using LINQ. Assuming that the search criteria is a List or array object.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Public Class Form1
    Public Shared mat As List(Of String) = Nothing
 
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
 
        mat = New List(Of String)
 
        mat.Add("Books")
        mat.Add("VB")
        mat.Add("Drinks")
        mat.Add("Food")
        mat.Add("Tea")
        mat.Add("Chod")
       
    End Sub
 
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If Not (mat Is Nothing) Then
            For Each tn As String In mat
                If (tvMat.Nodes.Find(tn, True).FirstOrDefault() IsNot Nothing) Then
                    If (tvMat.Nodes.Find(tn, True).FirstOrDefault().Checked) Then
                        MessageBox.Show(String.Format("Node Found"))
                        Me.Close()
                    End If
                End If
            Next
        End If
    End Sub
End Class

0 comments:

Post a Comment