What's wrong with this code?

  • Thread starter Thread starter **Developer**
  • Start date Start date
D

**Developer**

Dim tvn As TreeNode

For Each tvn In rootNode.Nodes

....SNIP

rootNode.Nodes.Remove(tvn)

Next



It does not remove all the nodes that it should remove.

I think when a node is removed it messes up the loop logic.





How can I fix it?







Thanks
 
You're exactly right. It's generally bad practice to modifiy a collection
that you are looping through. You can try looping numerically in reverse
order and always removing the last index--that should get them all. I don't
have .NET handy to test this, but something like (air code):

for n = tvn.rootnode.nodes.count - 1 to 0 step -1
'remove index: tvn.rootnode.nodes.count - 1
next n
 
This seems to work as well;
dim n as integer = 0
while n<tvn.rootnode.nodes.count
tvn.Rootnode.nodes(n).Remove
end while
 
What am I doing wrong?
TreeViewFolders is a TreeView
I replace the For... with
For lLpCnt as Integer = 0 to rootNode.Nodes.

(Note the dot at the end)

But InteliSense does not give me a Count property for Nodes





For Each tvn As TreeNode In TreeViewFolders.Nodes

InExpandedFolders(tvn, flags, confirmBefore, notifyFailure)

Next

----------------------------------------------

Private Sub InExpandedFolders(ByVal rootNode As TreeNode)

...SNIP

For Each tvn In rootNode.Nodes

..SNIP

If tvn.Checked Then

rootNode.Nodes.Remove(tvn)

End If

Next

End Sub
 
' Get the collection of TreeNodes.
Dim myNodeCollection As TreeNodeCollection = myTreeView.Nodes
Dim myCount As Integer = myNodeCollection.Count
 
But isn't Nodes a TreeNodeCollection??
So myNodeCollection references the same thing as Nodes?

Thanks
 
Tried it and it works.
But so does
rootNode.Nodes.Count

even though InteliSense does not offer that option!



Any enlightenment?
 
August 2, 2005

I have found that IntelliSense doesn't always list the correct
methods/properties available. I think it is a bug in MS's code or the fact
that they might not have added them to IntelliSense? I believe I have found
this a lot with ToString methods and I believe a Count property...

--
Joseph Bittman
Microsoft Certified Solution Developer

Web Site: http://71.39.42.23
Static IP
 
Dennis,

I prefer as well the reverse remove. In the solution that you use, you
cannot selective leave some nodes.

Just my 2 eurocents.

Cor
 
Joseph Bittman MCSD said:
August 2, 2005

I have found that IntelliSense doesn't always list the correct
methods/properties available. I think it is a bug in MS's code or the fact
that they might not have added them to IntelliSense? I believe I have
found this a lot with ToString methods and I believe a Count property...

Maybe you are hiding the "advanced members"?

Menu Tools -> Options -> Text-Editor -> Basic -> General: [ ] hide advanced
members


Armin
 
I would /almost/ always use the "reverse remove" as well ...

.... the exception being when there's a Clear method to hand... ;-)

Regards,
Phill W.
 
Phill,

I thought you where right, however you quoted to less.
... the exception being when there's a Clear method to hand... ;-)

Can you selective remove nodes with a clear. For the rest you are right.

:-)

Cor
 
Armin Zingler said:
Joseph Bittman MCSD said:
August 2, 2005

I have found that IntelliSense doesn't always list the correct
methods/properties available. I think it is a bug in MS's code or the
fact that they might not have added them to IntelliSense? I believe I
have found this a lot with ToString methods and I believe a Count
property...

Maybe you are hiding the "advanced members"?

Menu Tools -> Options -> Text-Editor -> Basic -> General: [ ] hide
advanced members


Armin

Exactly right in my case. I wonder why they consider "Count" as an
"advanced members"?

Now if someone could tell me how to get the IDE to show the RichTextBox drag
events (like RichTextBox1_DragEnter) in the Method Name drop down box I'd
consider today as exceptional.


Thanks a lot
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top