Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
200
TreeNode bug
posted

I get a system.arguentoutofrange exception with the following code.  Both conditional statements, unselecting and clearing, give this error.  If this were true the conditional statements would never let me get to that point.  This occurs when I have a form with a tree, I go to a dialgoue form where user can change the heirachry of the tree(save in a xml file) then upon closing the dialgue, I want to clear the tree on original page and rebuild from updated xml heirarchy file.  This seems to be a bug in your software.....

If Me.tvwReports.SelectedNodes.Count > 0 Then

If Not tvwReports.SelectedNodes(0) Is Nothing Then

Me.tvwReports.SelectedNodes(0).Selected = False

End If

End If

If tvwReports.Nodes.Count > 0 Then

Dim i As Integer = 0
While i <= tvwReports.Nodes.Count

tvwReports.Nodes(i).Remove()

i = i + 1

End While

End If

 

 

 

 

 

Parents
  • 469350
    Offline posted

    Are you certain that this is the exact code that gives you the error? 

    It should not be raising an exception, but there is a pretty big logical hole in this code: 

     

    If tvwReports.Nodes.Count > 0 Then

    Dim i As Integer = 0

    While i <= tvwReports.Nodes.Count

    tvwReports.Nodes(i).Remove()

    i = i + 1

    End While

     

    If you think about, you may see why.  You are removing nodes from the collection. This is going to change the count of the collection. But your index keeps going up and the Collection count keeps going down.

    So say you started with 3 selected nodes. 

    i is 0 and count is three. 

    So you remove node 0. 

    i is now 1 and count is 2. 

    So you remove node 1

    i is now 2 and count is 1

    At this point your While loop will exit because i is greater than the count. But there's still one selected node left in the collection, which does not appear to be the intention. 

     

    In any case, why don't you just call SelectedNodes.Clear?

Reply Children