How to search all nodes of TreeView ,TextBox With 'LINQ'

Mansour_Dalir 2,036 Reputation points
2023-07-12T05:27:11.16+00:00

hi

If the entered text is present in the content of the item, it will be colored.

    Dim MyDataTable As New DataTable
    Dim lstOfNodes As New List(Of TreeNode)
    Public dynamicColumn As String()
    Private Sub frmTreeView_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        MyDataTable.Columns.Add("Group")
        MyDataTable.Columns.Add("Type")
        MyDataTable.Columns.Add("Size")
        MyDataTable.Columns.Add("Location")
        MyDataTable.Rows.Add({"CABLE", "MV", "1x240", "Air Building"})
        MyDataTable.Rows.Add({"CABLE", "MV", "1x240", "ACC Building"})
        MyDataTable.Rows.Add({"CABLE", "LV", "1x240", "Swithgear "})
        MyDataTable.Rows.Add({"Install", "Cable Tray", "20", "Swithgear"})
        MyDataTable.Rows.Add({"Install", "Cable Tray", "30", "ACC"})
        MyDataTable.Rows.Add({"Install", "Cable Tray", "60", "ACC"})
        MyDataTable.Rows.Add({"Install", "JB", "10x10", "Admin"})
        MyDataTable.Rows.Add({"Install", "JB", "20x10", "Admin"})
        MyDataTable.Rows.Add({"Test", "Detector", "Smoke", "Admin"})
        MyDataTable.Rows.Add({"Test", "Detector", "Beam", "ACC"})
        MyDataTable.Rows.Add({"Test", "Detector", "Heat", "ACC"})
        Dim dynamicColumn = MyDataTable.Columns.Cast(Of DataColumn)().Select(Function(col) col.ColumnName).ToArray()
        Dim arrTree = (From row In MyDataTable.AsEnumerable()
                       Select dynamicColumn.Select(Function(h) row.Item(h)).ToArray).ToArray
        For Each item In arrTree
            Dim parentNode As TreeNode = Nothing
            For i = 0 To item.Length - 1
                Dim value1 = item(i).ToString()
                Dim node As TreeNode = Nothing
                If parentNode IsNot Nothing Then
                    node = parentNode.Nodes.Cast(Of TreeNode)() _
            .FirstOrDefault(Function(n) n.Text = value1)
                Else
                    node = TreeView1.Nodes.Cast(Of TreeNode)() _
            .FirstOrDefault(Function(n) n.Text = value1)
                End If
                If node IsNot Nothing Then
                    node.ForeColor = Color.FromArgb(0, 0, 0, 0)
                End If
                If node Is Nothing Then
                    node = New TreeNode(value1)
                    If parentNode IsNot Nothing Then
                        parentNode.Nodes.Add(node)
                    Else
                        TreeView1.Nodes.Add(node)
                    End If
                End If
                parentNode = node
            Next
        Next
    End Sub
    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        'Need Function To Find  
    End Sub
Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2023-07-12T09:08:59.6366667+00:00

    Hi @Mansour_Dalir ,

    You can refer to the following code.

    Imports System.Runtime.CompilerServices
    
    
    Public Class Form1
        Dim MyDataTable As New DataTable
        Public dynamicColumn As String()
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            MyDataTable.Columns.Add("Group")
            MyDataTable.Columns.Add("Type")
            MyDataTable.Columns.Add("Size")
            MyDataTable.Columns.Add("Location")
            MyDataTable.Rows.Add({"CABLE", "MV", "1x240", "Air Building"})
            MyDataTable.Rows.Add({"CABLE", "MV", "1x240", "ACC Building"})
            MyDataTable.Rows.Add({"CABLE", "LV", "1x240", "Swithgear "})
            MyDataTable.Rows.Add({"Install", "Cable Tray", "20", "Swithgear"})
            MyDataTable.Rows.Add({"Install", "Cable Tray", "30", "ACC"})
            MyDataTable.Rows.Add({"Install", "Cable Tray", "60", "ACC"})
            MyDataTable.Rows.Add({"Install", "JB", "10x10", "Admin"})
            MyDataTable.Rows.Add({"Install", "JB", "20x10", "Admin"})
            MyDataTable.Rows.Add({"Test", "Detector", "Smoke", "Admin"})
            MyDataTable.Rows.Add({"Test", "Detector", "Beam", "ACC"})
            MyDataTable.Rows.Add({"Test", "Detector", "Heat", "ACC"})
            Dim dynamicColumn = MyDataTable.Columns.Cast(Of DataColumn)().Select(Function(col) col.ColumnName).ToArray()
            Dim arrTree = (From row In MyDataTable.AsEnumerable()
                           Select dynamicColumn.Select(Function(h) row.Item(h)).ToArray).ToArray
            For Each item In arrTree
                Dim parentNode As TreeNode = Nothing
                For i = 0 To item.Length - 1
                    Dim value1 = item(i).ToString()
                    Dim node As TreeNode = Nothing
                    If parentNode IsNot Nothing Then
                        node = parentNode.Nodes.Cast(Of TreeNode)() _
                .FirstOrDefault(Function(n) n.Text = value1)
                    Else
                        node = TreeView1.Nodes.Cast(Of TreeNode)() _
                .FirstOrDefault(Function(n) n.Text = value1)
                    End If
                    If node IsNot Nothing Then
                        node.ForeColor = Color.FromArgb(0, 0, 0, 0)
    
                    End If
                    If node Is Nothing Then
                        node = New TreeNode(value1)
                        If parentNode IsNot Nothing Then
                            parentNode.Nodes.Add(node)
                        Else
                            TreeView1.Nodes.Add(node)
                        End If
                    End If
                    parentNode = node
                Next
            Next
        End Sub
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            Dim allNodes = TreeView1.Nodes.Find("", True).AsEnumerable()
            allNodes.ToList().ForEach(Sub(node) node.BackColor = Color.White)
            Dim nodes = TreeView1.FlattenTree().Where(Function(n) n.Text = TextBox1.Text).ToList()
            For Each tn As TreeNode In nodes
                tn.BackColor = Color.Red
            Next
        End Sub
    
    End Class
    
    
    Module SOExtension
        <Extension()>
        Function FlattenTree(ByVal tv As TreeView) As IEnumerable(Of TreeNode)
            Return FlattenTree(tv.Nodes)
        End Function
    
        <Extension()>
        Function FlattenTree(ByVal coll As TreeNodeCollection) As IEnumerable(Of TreeNode)
            Return coll.Cast(Of TreeNode)().Concat(coll.Cast(Of TreeNode)().SelectMany(Function(x) FlattenTree(x.Nodes)))
        End Function
    End Module
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.