Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

VB.net Expand TreeView at first child level

Hi. I am using the code below to fill a TreeView. What code would I use
to expand it only at the first child level as shown in the image
User generated imageSub A()
        Dim xml_doc As New XmlDocument
        Dim xml_node As XmlNode
        Dim i As Integer
        Dim oCount As Integer = 0
        Dim arrKEY(10000) As String
        Dim arrDescription(10000) As String
        Dim arrParent(10000) As String
        Dim arrFolderOrReport(10000) As String
        Dim arrImage(10000) As String
        Dim oArrayIndex As Integer = -1
        Dim pCount As Integer = 0

        xml_doc.Load("C:\Users\user\Documents\REPORTS.xml")
        xml_node = xml_doc.DocumentElement

        For Each child_node As XmlNode In xml_node.ChildNodes

            oCount = oCount + 1
            arrKEY(oCount) = child_node.Attributes.GetNamedItem("key").InnerText
            arrFolderOrReport(oCount) = child_node.Name  'FOLDER or REPORT
            For i = 0 To child_node.ChildNodes.Count - 1
                If child_node.ChildNodes(i).Name = "DESCRIPTION" Then
                    arrDescription(oCount) = child_node.ChildNodes(i).InnerText
                ElseIf child_node.ChildNodes(i).Name = "PARENT" Then
                    arrParent(oCount) = child_node.ChildNodes(i).InnerText
                ElseIf child_node.ChildNodes(i).Name = "IMAGE" Then
                    arrImage(oCount) = child_node.ChildNodes(i).InnerText
                End If
            Next

        Next child_node

        TreeView1.BeginUpdate()

        For g = 0 To oCount

            Dim tns() As TreeNode
            If arrKEY(g) = "" Or arrKEY(g) = Nothing Or arrKEY(g) = "-- Bottom Row --" Then GoTo Jump
            If arrParent(g) = "" Then
                TreeView1.Nodes.Add(arrKEY(g), arrDescription(g))
            Else

                tns = TreeView1.Nodes.Find(arrParent(g), True)

                If tns.Length > 0 Then
                    tns(0).Nodes.Add(arrKEY(g), arrDescription(g))
                Else
                    MessageBox.Show("Parent Node Not Found")
                End If
            End If

Jump:
        Next

        TreeView1.EndUpdate()

        TreeView1.CheckBoxes = True

    End Sub
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
For Each tn As TreeNode In treeView1.Nodes
	If tn.Level = 1 Then
		tn.Expand()
	End If
Next

Open in new window

Avatar of Murray Brown

ASKER

thanks