VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,768 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
hi Please add one (TreeView) and one (TextBox) to the form. Thank you Going between branches (.) and increasing the filter (Space)
If possible, give a better answer through coding with (LINQ).
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"})
TreeViewFilter(TextBox1.Text, MyDataTable, TreeView1)
me.Text="Going between branches (.) and increasing the filter (Space)"
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
TreeViewFilter(TextBox1.Text, MyDataTable, TreeView1)
End Sub
Private Sub TreeViewFilter(inText As String, InDT As DataTable, InTreeView As TreeView)
Dim NodesSetColor() = Nothing
dynamicColumn = InDT.Columns.Cast(Of DataColumn)().Select(Function(col) col.ColumnName).ToArray()
Dim SplDot As String() = Split(inText, ".")
Dim sttFilter As String = ""
Dim sttF2 As String = ""
Dim Level As Byte = 0
For a = 0 To SplDot.Length - 1
If SplDot(a) = "" Then Continue For
If Level < dynamicColumn.Length Then
Level += 1
Else
Exit For
End If
Dim splSpace As String() = Split(SplDot(a), " ")
If splSpace.Length > 1 Then
For b = 0 To splSpace.Length - 1
sttF2 += IIf(b > 0, " And ", "") & "[" & dynamicColumn(Level - 1) & "] Like '*" & splSpace(b) & "*'"
Next
sttFilter += IIf(a > 0, " And ", "") & sttF2
sttF2 = ""
Else
sttFilter += IIf(a > 0, " And ", "") & "[" & dynamicColumn(Level - 1) & "] Like '*" & SplDot(a) & "*'"
End If
Next
InDT.DefaultView.RowFilter = sttFilter
Dim DtFilterForFunction As DataTable = InDT.DefaultView.ToTable.Copy
Me.Text = sttFilter
Dim arrTree = (From row In InDT.DefaultView.ToTable.AsEnumerable()
Select dynamicColumn.Select(Function(h) row.Item(h)).ToArray).ToArray
Dim sttDistinct As String() = Nothing
If Level > 0 Then
sttDistinct = InDT.DefaultView.ToTable.AsEnumerable().Select(Function(f) CStr(f.Item(dynamicColumn(Level - 1)))).Distinct.ToArray
End If
If arrTree.Length = 1 Then
' NodesSetColor = arrTree(0) ' RowTreViewFilter.ItemArray
InTreeView.BackColor = Color.LightGreen
Else
InTreeView.BackColor = Color.White
End If
If Level = 0 Then lstOfNodes.Clear()
arrTree = (From row In InDT.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 = InTreeView.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
InTreeView.Nodes.Add(node)
End If
End If
parentNode = node
Next
Next
InTreeView.CollapseAll()
lstOfNodes = GetNodes(dynamicColumn, DtFilterForFunction, Level)
For Each itemList In lstOfNodes
itemList.Expand()
Next
If lstOfNodes.Count > 0 Then
TreeView1.SelectedNode = lstOfNodes(lstOfNodes.Count - 1)
lstOfNodes(lstOfNodes.Count - 1).ForeColor = Color.Red
End If
End Sub
Private Function GetNodes(dynamicColumn As String(), DtFilterResult As DataTable, level As Byte) As List(Of TreeNode)
Dim lstTree As New List(Of TreeNode)
For a = 0 To level - 1
Dim r1 As String = dynamicColumn(a)
Dim ValuesOneColumn = DtFilterResult.AsEnumerable().Select((Function(k) k.Item(r1))).Distinct.ToArray
If ValuesOneColumn.Length = 1 Then
Dim Nodes
Dim sttTexts
If lstTree.Count > 0 Then
Nodes = lstTree(lstTree.Count - 1).Nodes.Cast(Of TreeNode).Select(Function(h) h).ToArray
sttTexts = lstTree(lstTree.Count - 1).Nodes.Cast(Of TreeNode).Select(Function(h) h.Text).ToArray
Else
Nodes = TreeView1.Nodes.Cast(Of TreeNode).Select(Function(h) h).ToArray
sttTexts = TreeView1.Nodes.Cast(Of TreeNode).Select(Function(h) h.Text).ToArray
End If
Dim idxOf As Integer = Array.IndexOf(sttTexts, ValuesOneColumn(0))
If idxOf <> -1 Then
If lstTree.Contains(Nodes(idxOf)) = False Then
lstTree.Add(Nodes(idxOf))
End If
End If
End If
Next
Return lstTree
End Function
I'm sure there is a short answer to this question .I need a short answer. I wrote very busy.
Here is the code inLINQ
Imports System.Data
Public Class frmTreeView 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 ' Initialize the DataTable and add data
MyDataTable.Columns.Add("Group")
MyDataTable.Columns.Add("Type")
MyDataTable.Columns.Add("Size")
MyDataTable.Columns.Add("Location")
MyDataTable.Rows.Add({"CABLE", "MV", "1x240", "Air Building"})
' Add more rows... '
Set up the initial state of the TreeView
TreeViewFilter(TextBox1.Text, MyDataTable, TreeView1)
Me.Text = "Going between branches (.) and increasing the filter (Space)" End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged ' Filter the TreeView based on the TextBox value
TreeViewFilter(TextBox1.Text, MyDataTable, TreeView1) End Sub Private Sub TreeViewFilter(inText As String, InDT As DataTable, InTreeView As TreeView) ' Filtering logic using LINQ ' ... ' Implement your filtering logic here using LINQ queries on the DataTable ' ... ' Update the TreeView based on the filtered data ' ... ' Implement your code to update the TreeView with the filtered data ' ... End Sub Private Sub SyngList(inNodes As TreeNode, value1 As Object, Level As Byte, sttDistinct As String()) ' Sync the list of nodes based on the filtering result ' ... ' Implement your code to synchronize the list of nodes based on the filtering result ' ... End Sub End Class
If you find it useful kindly accept answer