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
If tvwReports.Nodes.Count > 0 Then
tvwReports.Nodes(i).Remove()
i = i + 1
End While
I don't have a call stack to show, but I can confirm that you're not the only one to have seen this problem. I am using your methodology to remove the selected nodes one-by-one and that gets the job done.
If SelectedNodes.Clear results in an error, then something is clearly wrong.Is the error occuring on the line of code where you call the Clear method? Or is it, perhaps, occurring in an event handler triggered by this call?
What does the call stack look like when the exception occurs? Can you post it here?
Yes, sorry, that is not the correct code. I did remove the i = i+1 line.
Anyways, the SelectedNodes.Clear rasied the error, as well as tvwReports.Nodes.Clear raised and error, index out of range. I got it to work if i iterate through the nodes and remove them one my one. But shouldn't tvwReports.Nodes.Clear and SelectedNodes.Clear work in this case as well?
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:
While i <= tvwReports.Nodes.Count
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?