How to get the number of sub-branches of a TreeView

Mansour_Dalir 2,016 Reputation points
2023-08-21T16:14:55.6366667+00:00

hi

It should be a function that can count the number of sub-branches of the tree view on the headers

addCount

T

Need code changes. Thank.

Public Class test2
    Dim MyDataTable As New DataTable
    Dim lstColumns As New List(Of String)
    Private Sub test2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Add DataGridView,TreeView1 on Form
        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({"", "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", "", "Smoke", "Admin"})
        MyDataTable.Rows.Add({"Test", "", "Beam", "ACC"})
        MyDataTable.Rows.Add({"Test", "Detector", "Heat", "ACC"})
        DataGridView1.DataSource = MyDataTable
        DataGridView1.AllowUserToOrderColumns = True
    End Sub
    Private Sub SetTreeView()
        Dim arrTree = (From row In MyDataTable.AsEnumerable()
                       Select lstColumns.Select(Function(h) row.Item(h)).ToArray).ToArray
        For a = 0 To arrTree.Length - 1
            Dim parentNode As TreeNode = Nothing
            For b = 0 To arrTree(a).Length - 1
                Dim value1 = arrTree(a)(b).ToString()
                If value1 = "" Then value1 = "Empty Value"
                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)
                    ' node.Tag = dynamicColumn(b) & "|" & InDT.Rows(a).Item(DgvFrom.ColumnPrimaryKey)

                    If parentNode IsNot Nothing Then
                        parentNode.Nodes.Add(node)
                    Else
                        TreeView1.Nodes.Add(node)
                    End If
                End If
                parentNode = node
            Next
        Next
        TreeView1.CollapseAll()
    End Sub

    Private Sub DataGridView1_ColumnDisplayIndexChanged(sender As Object, e As DataGridViewColumnEventArgs) Handles DataGridView1.ColumnDisplayIndexChanged
        Dim g As DataGridView = DirectCast(sender, DataGridView)
        Dim propertyInfo As Reflection.PropertyInfo = GetType(DataGridViewColumn).GetProperty("DisplayIndexHasChanged", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)

        If g.Columns.Cast(Of DataGridViewColumn)().Any(Function(x) DirectCast(propertyInfo.GetValue(x), Boolean)) Then
            Return
        Else
            Dim columnTitlesArray() As String = DataGridView1.Columns.Cast(Of DataGridViewColumn)() _
                                           .OrderBy(Function(column) column.DisplayIndex) _
                                           .Select(Function(column) column.HeaderText) _
                                           .ToArray()
            TreeView1.Nodes.Clear()
            lstColumns.Clear()
            For Each columnTitle As String In columnTitlesArray
                lstColumns.Add(columnTitle)
                '  Console.WriteLine(columnTitle)
            Next
        End If
        SetTreeView()
    End Sub
End Class
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,779 questions
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 33,451 Reputation points Microsoft Vendor
    2023-08-22T01:53:12.38+00:00

    Hi @Mansour_Dalir ,

    Please check if the following code helps.

    1. Iterate over each node in the TreeView.
    2. For each node, recursively traverse its children, and then calculate the minimum number of nodes contained in the children.
    3. Accumulates the minimum node count of child nodes to the current node.
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For Each node As TreeNode In TreeView1.Nodes
                CalculateMinNodeCount(node)
            Next
        End Sub
    
        Private Sub CalculateMinNodeCount(node As TreeNode)
    
            If node.Nodes.Count = 0 Then
                node.Tag = 1
                node.Text &= " (1)"
                Return
            End If
    
            Dim nodeCount As Integer = 0
    
            For Each childNode As TreeNode In node.Nodes
                CalculateMinNodeCount(childNode)
                nodeCount += CInt(childNode.Tag)
            Next
    
            node.Tag = nodeCount
            node.Text &= " (" & nodeCount & ")"
        End Sub
    

    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.


1 additional answer

Sort by: Most helpful
  1. Dewayne Basnett 1,371 Reputation points
    2023-08-21T16:44:22.2166667+00:00

    Try this. I think you are looking for .Nodes.

    Public Class Form1
    
        Private Sub ChildNodes(tn As TreeNode)
            tn.Text &= String.Format(" ({0})", tn.Nodes.Count)
            For Each nd As TreeNode In tn.Nodes
                ChildNodes(nd)
            Next
        End Sub
    
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            ChildNodes(TreeView1.Nodes(0))
        End Sub
    End Class
    
    
    0 comments No comments

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.