Click here to Skip to main content
15,843,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to attain Expanding and Collapsing the Treeview by level in normal windows forms control.please explain with vb.net or c# code.
Posted

Can you provide a screenshot of what exactly you want.
 
Share this answer
 
Comments
Madhukumar N 4-Sep-14 4:53am    
Here is what i Need

I want to just give the level as input and expand that particular level of nodes from the Treeview
I found out the solution Myself

Public Class Form1

   Dim CurrentLevel As Integer
   Dim MaxLevel As Integer

   Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

      With TreeView1
         .BeginUpdate()
         .Nodes.Add("Account")
         .Nodes(0).Nodes.Add("PORT: My Work Services")
         .Nodes(0).Nodes(0).Nodes.Add("OFF: Global Field Services")
         .Nodes(0).Nodes(0).Nodes(0).Nodes.Add("ITSVC: Moves")
         .Nodes(0).Nodes(0).Nodes(0).Nodes.Add("ITSVC: Installs")
         .Nodes(0).Nodes(0).Nodes(0).Nodes.Add("ITSVC: Maintenance")
         .Nodes(0).Nodes(0).Nodes.Add("OFF: Service Desk")
         .Nodes(0).Nodes(0).Nodes(1).Nodes.Add("ITSVC: Incident Management")
         .Nodes(0).Nodes(0).Nodes(1).Nodes.Add("ITSVC: Change Management")
         .Nodes(0).Nodes(0).Nodes(1).Nodes.Add("ITSVC: Problem Management")

         .Nodes(0).Nodes.Add("PORT: Data Center")
         .Nodes(0).Nodes(1).Nodes.Add("OFF: Mainframe")
         .Nodes(0).Nodes(1).Nodes(0).Nodes.Add("ITSVC: Applications")
         .Nodes(0).Nodes(1).Nodes(0).Nodes.Add("ITSVC: Database")
         .Nodes(0).Nodes(1).Nodes(0).Nodes.Add("ITSVC: Web")
         .Nodes(0).Nodes(1).Nodes.Add("OFF: Midrange")
         .Nodes(0).Nodes(1).Nodes(1).Nodes.Add("ITSVC: App")
         .Nodes(0).Nodes(1).Nodes(1).Nodes.Add("ITSVC: DB")
         .Nodes(0).Nodes(1).Nodes(1).Nodes.Add("ITSVC: Internet")

         .EndUpdate()

         .Nodes(0).ExpandAll()
      End With

      MaxLevel = 3
      CurrentLevel = 3

   End Sub

   ' Call the procedure using the top nodes of the treeview.
   Private Sub ExpandToLevel(Direction As String, Optional SpecificLevel As Integer = -1)

      Dim Node As TreeNode

      TreeView1.BeginUpdate() 'BeginUpdate() and EndUpdate() seems to be the equivalent of Tlist.Redraw = True and False

      If SpecificLevel = -1 Then
         Select Case Direction
            Case "Expand"
               If CurrentLevel < MaxLevel Then
                  CurrentLevel += 1
               End If
            Case "Collapse"
               If CurrentLevel > 0 Then
                  CurrentLevel -= 1
               End If
         End Select
      Else
         CurrentLevel = SpecificLevel
      End If

      For Each Node In TreeView1.Nodes 'Always level 0
         If CurrentLevel = 0 Then
            Node.Collapse()
         Else
            Node.ExpandAll()
            ExpandToLevelRecursive(Node)
         End If
      Next

      TreeView1.EndUpdate()

   End Sub

   Private Sub ExpandToLevelRecursive(ByVal ParentNode As TreeNode)

      Dim ChildNode As TreeNode

      For Each ChildNode In ParentNode.Nodes
         Debug.Print(ChildNode.Text)
         If ChildNode.Level = CurrentLevel Then
            ChildNode.Collapse()
         Else
            ExpandToLevelRecursive(ChildNode)
         End If
      Next

   End Sub

   Private Sub ExpandToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExpandToolStripMenuItem.Click
      ExpandToLevel("Expand")
   End Sub

   Private Sub CollapseToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CollapseToolStripMenuItem.Click
      ExpandToLevel("Collapse")
   End Sub

   Private Sub AccountToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AccountToolStripMenuItem.Click
      ExpandToLevel("Not Used By the ExpandToLevel Sub", 0)
   End Sub

   Private Sub PortfolioToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles PortfolioToolStripMenuItem.Click
      ExpandToLevel("Not Used By the ExpandToLevel Sub", 1)
   End Sub

   Private Sub OfferingToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OfferingToolStripMenuItem.Click
      ExpandToLevel("Not Used By the ExpandToLevel Sub", 2)
   End Sub

   Private Sub ITServiceToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ITServiceToolStripMenuItem.Click
      ExpandToLevel("Not Used By the ExpandToLevel Sub", 3)
   End Sub

End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900