Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. Treeview iteration

Treeview iteration

Scheduled Pinned Locked Moved Windows Forms
designdata-structureshelpannouncement
3 Posts 2 Posters 15 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 9285131
    wrote on last edited by
    #1

    I have a tree created at design time with 3 root nodes and 3 levels 3x3x3 =39 nodes as the default.

    The nodes can be dynamically recreated.

    When I cleared the nodes then recreated them, I tried looping through to add properties to the nodes of each level including their relevant context menu name.

    Private Sub RedoTree\_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RedoTree.Click
    
    	treeLoad()
    	treeExecutive.ExpandAll()
    	restoreTP()
    	treeExecutive.Nodes(0).EnsureVisible()
    
    End Sub
    
    Sub restoreTP() '				renew treenode properties
    
    	Dim s As String, l As Short
    
    	treeExecutive.Update()
    
    	For Each treenode In treeExecutive.Nodes
    		s = treenode.Name
    		l = Len(s)
    		treenode.ForeColor = Color.GhostWhite
    
    		If treenode.Text <> s Then treenode.ForeColor = Color.Black
    
    		Select Case l
    			Case 1
    				treenode.NodeFont = New Font("Arial Rounded MT Bold", 10.8)
    				treenode.ContextMenuStrip = CMSRoot
    			Case 2
    				treenode.NodeFont = New Font("Arial", 10.2)
    				treenode.ContextMenuStrip = CMSTree
    			Case Else
    				treenode.NodeFont = New Font("Arial Narrow", 9)
    				treenode.ContextMenuStrip = CMSTree
    		End Select
    
    	Next
    
    End Sub
    

    The root nodes are number 1-3, then 11,12,13 .... 111,112,113 Total is 39 nodes

    ONLY THE ROOT NODES ARE RECOGNISED AND DEALT WITH.

    All the child nodes are ignored as if they don't exist

    Am bogged down on this and would appreciate any help :confused:

    Demac

    Richard DeemingR 1 Reply Last reply
    0
    • U User 9285131

      I have a tree created at design time with 3 root nodes and 3 levels 3x3x3 =39 nodes as the default.

      The nodes can be dynamically recreated.

      When I cleared the nodes then recreated them, I tried looping through to add properties to the nodes of each level including their relevant context menu name.

      Private Sub RedoTree\_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RedoTree.Click
      
      	treeLoad()
      	treeExecutive.ExpandAll()
      	restoreTP()
      	treeExecutive.Nodes(0).EnsureVisible()
      
      End Sub
      
      Sub restoreTP() '				renew treenode properties
      
      	Dim s As String, l As Short
      
      	treeExecutive.Update()
      
      	For Each treenode In treeExecutive.Nodes
      		s = treenode.Name
      		l = Len(s)
      		treenode.ForeColor = Color.GhostWhite
      
      		If treenode.Text <> s Then treenode.ForeColor = Color.Black
      
      		Select Case l
      			Case 1
      				treenode.NodeFont = New Font("Arial Rounded MT Bold", 10.8)
      				treenode.ContextMenuStrip = CMSRoot
      			Case 2
      				treenode.NodeFont = New Font("Arial", 10.2)
      				treenode.ContextMenuStrip = CMSTree
      			Case Else
      				treenode.NodeFont = New Font("Arial Narrow", 9)
      				treenode.ContextMenuStrip = CMSTree
      		End Select
      
      	Next
      
      End Sub
      

      The root nodes are number 1-3, then 11,12,13 .... 111,112,113 Total is 39 nodes

      ONLY THE ROOT NODES ARE RECOGNISED AND DEALT WITH.

      All the child nodes are ignored as if they don't exist

      Am bogged down on this and would appreciate any help :confused:

      Demac

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      The TreeView's Nodes property[^] only returns the root nodes. Each node has its own Nodes property[^] containing its child nodes. To iterate through all nodes in the tree, you will need a recursive method. For example:

      Sub restoreTP()
      treeExecutive.BeginUpdate()
      Try
      restoreTP(treeExecutive.Nodes)
      Finally
      treeExecutive.EndUpdate()
      End Try
      End Sub

      Sub restoreTP(ByVal nodes As TreeNodeCollection)
      Dim s As String, l As Integer
      For Each treenode As TreeNode in nodes
      ...

          restoreTP(treenode.Nodes)
      Next
      

      End Sub


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      U 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        The TreeView's Nodes property[^] only returns the root nodes. Each node has its own Nodes property[^] containing its child nodes. To iterate through all nodes in the tree, you will need a recursive method. For example:

        Sub restoreTP()
        treeExecutive.BeginUpdate()
        Try
        restoreTP(treeExecutive.Nodes)
        Finally
        treeExecutive.EndUpdate()
        End Try
        End Sub

        Sub restoreTP(ByVal nodes As TreeNodeCollection)
        Dim s As String, l As Integer
        For Each treenode As TreeNode in nodes
        ...

            restoreTP(treenode.Nodes)
        Next
        

        End Sub


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        U Offline
        U Offline
        User 9285131
        wrote on last edited by
        #3

        Hi thx Richard for that info Not familiar with recursion, but now you've helped me improve my understanding of treeviews. I did circumvent the problem by updating node properties as they are created. I can post that code if it might help others. demac ;)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups