Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6051659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:50:34+00:00 2026-05-23T07:50:34+00:00

I have a ASP.net page that contains a TreeView that is updated dynamically. I

  • 0

I have a ASP.net page that contains a TreeView that is updated dynamically. I have encountered a problem using the TreeNodeCollection that I can not figure out the reasons behind.

The following code is a much simplified replication of the issue, when the page_load event fires a treeview control is created with a root node, then a function is called that returns a collection of nodes and subnodes. A For Next loops traverses the collection and adds the nodes to the root node. The TreeView control is then added to the page. The example below works as I expected.

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim treeView1 As New TreeView
        treeView1.Nodes.Add(New TreeNode)

        Dim nodeCollection As TreeNodeCollection = GetNodes() 

        For nodeIndex = 0 To nodeCollection.Count - 1
            treeView1.Nodes(0).ChildNodes.Add(nodeCollection(nodeIndex))
        Next

        Me.Form.Controls.Add(treeView1)
    End Sub

    Function GetNodes() As TreeNodeCollection
        Dim tnc As New TreeNodeCollection, tn As New TreeNode, sn As New TreeNode
        For i = 0 To 4
            tn = New TreeNode("Node" & i)
            tn.ChildNodes.Add(New TreeNode("Subnode1"))
            tn.ChildNodes.Add(New TreeNode("Subnode2"))
            tn.ChildNodes.Add(New TreeNode("Subnode3"))
            tnc.Add(tn)
        Next
        Return tnc
    End Function

To replicate the problem I change the line Return tnc in the GetNodes() function with Return tnc(1).ChildNodes

The function still returns a valid TreeNodeCollection of the three sub nodes that were initially added to Node1.

Now when the codes begins to traverse the collection, in the For Next loop each time a node is added to treeView1 control it is removed from the nodeCollection??? This subsequently messes up for next loop and an Index was out of range error is thrown.

Why after the code change are nodes moved from the collection, when in the initial example the nodeCollection retains all its items.

EDIT

If I change the For Next loop to a For Each loop then the exception is slightly different e.g

For Each thisNode AS TreeNode In nodeCollection
    treeView1.Nodes(0).ChildNodes.Add(thisNode)
Next

generates the exception

Collection was modified; enumeration operation may not execute.

Which kind of makes sense as the current node (for whatever reason) is being moved from the source collection to the treeview. But why doesn’t this happen when Return tnc is used?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:50:34+00:00Added an answer on May 23, 2026 at 7:50 am

    A TreeNode can belong to only one parent TreeView or parent TreeNode at a given point in time. When you take a TreeNode that belongs to one “tree” and add it to another “tree”, it will automatically be removed from its previous owner.

    So then why the different between returning the outer node collection as opposed to the child node’s sub-collection? I believe this difference is caused because the outer collection has no owner, so it has no owner from which it can be removed. In the case of the child node’s sub-collection they all belong to the parent node, so their relationship is able to be tracked.

    Here’s a little diagram to show the difference:

    TreeNodeCollection
        Node 1 (owner=<none>)
            TreeNodeCollection
                Node 1.1 (owner=Node 1)
                Node 1.2 (owner=Node 1)
                Node 1.3 (owner=Node 1)
                Node 1.4 (owner=Node 1)
        Node 2 (owner=<none>)
        Node 3 (owner=<none>)
        Node 4 (owner=<none>)
    

    So this issue would only happen with nodes that have owners.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.Net page that contains a <div> with an <img> tag within.
I have a ASP.NET web page that contains many textboxes. Each textbox has a
I have an ASP.Net 2.0 page that contains two UpdatePanels. The first panel contains
I have a master page that contains an ASP.NET server side Menu control (System.Web.UI.WebControls.Menu)
I have a page which contains a html.RenderPartial, that renders an ASP.NET MVC partial
I have a usercontrol that is in an asp.net page that contains a button.
I have an ambitious requirement for an asp.net 2.0 web page that contains a
I have an ASP.NET Page that contains a User control called ReportCtrl (my own
I have master page that contains the following asp.net form tag <form id=CommerceMasterForm runat=server>
I have an ASP.NET page that contains a form and a button. In order

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.