By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,398 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,398 IT Pros & Developers. It's quick & easy.

Newb needs a little help

P: n/a
you
Ok, I hope that I can 'splain this right. I am very new so bear with me.

I have a treeview with its main node being a user selected folder. In
this treeview you can only see subfolders and two specific file types.
What I am trying to do next is to remove any subfolders from the tree
that bo not have any of the two file types. This is done to somewhat
unclutter the tree.

Here is my code so far to clear the folders out. It is called with the
topmost node as the inputnode to start with.

Private Sub RemoveUselessFolders(inputnode as TreeNode)
Try
Dim node As TreeNode
For Each node In inputnode.Nodes
If node.ImageIndex < 2 Then 'it is a folder
If node.Nodes.Count = 0 Then 'it has no subfolders-can remove
node.Remove
Else
RemoveUselessFolders(node)
End If
End If
Next
Catch ex As system.NullReferenceException
End Try
End Sub
This seems to throw NullReferenceException's and maybe deletes a folder
and maybe not. I just do not understand why this does not work. I do not
have VS, so I can not step through it and see what is (or isn't)
happening. Anyone have a clue?

If you need more info please let me know.

Thanks,
Jason
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
You shouldn't modify a collection while using the foreach loop to iterate
through it because that uses an enumerator under the hood which cannot work
correctly if the collection it is going through is being modified while it
is used.
Use another type of loop.

"you" <yo*@me.com> wrote in message
news:MP************************@news.microsoft.com ...
Ok, I hope that I can 'splain this right. I am very new so bear with me.

I have a treeview with its main node being a user selected folder. In
this treeview you can only see subfolders and two specific file types.
What I am trying to do next is to remove any subfolders from the tree
that bo not have any of the two file types. This is done to somewhat
unclutter the tree.

Here is my code so far to clear the folders out. It is called with the
topmost node as the inputnode to start with.

Private Sub RemoveUselessFolders(inputnode as TreeNode)
Try
Dim node As TreeNode
For Each node In inputnode.Nodes
If node.ImageIndex < 2 Then 'it is a folder
If node.Nodes.Count = 0 Then 'it has no subfolders-can remove
node.Remove
Else
RemoveUselessFolders(node)
End If
End If
Next
Catch ex As system.NullReferenceException
End Try
End Sub
This seems to throw NullReferenceException's and maybe deletes a folder
and maybe not. I just do not understand why this does not work. I do not
have VS, so I can not step through it and see what is (or isn't)
happening. Anyone have a clue?

If you need more info please let me know.

Thanks,
Jason

Nov 21 '05 #2

P: n/a
Private Sub RemoveUselessFolders(inputnode as TreeNode)
Dim i as integer
For i= inputNodes.Count -1 to 0 Step -1
With inputNodes(i)
If .ImageIndex < 2 Then
If .Nodes.Count = 0 Then
.Remove
Else
RemoveUselessFolders(inputNodes(i))
End If
End If
End With
Next i
End Sub

Note i would not rely on .ImageIndex < 2 to index a folder the link is too weak and can easily be
broken. Either subclass the treenode to make a DirectoryTreeNode or create an Enum

Public Enum NodeType As Integer
Directory = 1
File = 2
End Enum

On creating the nodes set inputNode.Tag = NodeType.Directory such that this
If .ImageIndex < 2 Then
statement becomes
If ctype(.ImageIndex.Tag, NodeType) = NodeType.Directory Then
Another thing to think about is why do first fill the tree with a bunch of nodes that you then
prune. Why not do the check as you are adding th nodes such that you dont add them if
inputNodes(i).Nodes.Count = 0 ?
hth
Richard
"Marina" <so*****@nospam.com> wrote in message news:uL**************@TK2MSFTNGP14.phx.gbl...
You shouldn't modify a collection while using the foreach loop to iterate
through it because that uses an enumerator under the hood which cannot work
correctly if the collection it is going through is being modified while it
is used.
Use another type of loop.

"you" <yo*@me.com> wrote in message
news:MP************************@news.microsoft.com ...
Ok, I hope that I can 'splain this right. I am very new so bear with me.

I have a treeview with its main node being a user selected folder. In
this treeview you can only see subfolders and two specific file types.
What I am trying to do next is to remove any subfolders from the tree
that bo not have any of the two file types. This is done to somewhat
unclutter the tree.

Here is my code so far to clear the folders out. It is called with the
topmost node as the inputnode to start with.

Private Sub RemoveUselessFolders(inputnode as TreeNode)
Try
Dim node As TreeNode
For Each node In inputnode.Nodes
If node.ImageIndex < 2 Then 'it is a folder
If node.Nodes.Count = 0 Then 'it has no subfolders-can remove
node.Remove
Else
RemoveUselessFolders(node)
End If
End If
Next
Catch ex As system.NullReferenceException
End Try
End Sub
This seems to throw NullReferenceException's and maybe deletes a folder
and maybe not. I just do not understand why this does not work. I do not
have VS, so I can not step through it and see what is (or isn't)
happening. Anyone have a clue?

If you need more info please let me know.

Thanks,
Jason


Nov 21 '05 #3

P: n/a
You,

In addition to Marina,

You can use the for each loop in this kind of routines.
However set it in a do until loop and exit when you have done an addition or
whatever. By instance this

\\\
Do until ("ready state is reached")
for each in collection
if condition then
'do what you have to do
exit for
end sub
next
loop
///

This can help you in a lot of cases, however do not do this when you are
sure that it will be with a lot of processing while a control is involved.
It is almost the same as a classic arraysort.

I hope this helps?

Cor

"you" <yo*@me.com> schreef in bericht
news:MP************************@news.microsoft.com ...
Ok, I hope that I can 'splain this right. I am very new so bear with me.

I have a treeview with its main node being a user selected folder. In
this treeview you can only see subfolders and two specific file types.
What I am trying to do next is to remove any subfolders from the tree
that bo not have any of the two file types. This is done to somewhat
unclutter the tree.

Here is my code so far to clear the folders out. It is called with the
topmost node as the inputnode to start with.

Private Sub RemoveUselessFolders(inputnode as TreeNode)
Try
Dim node As TreeNode
For Each node In inputnode.Nodes
If node.ImageIndex < 2 Then 'it is a folder
If node.Nodes.Count = 0 Then 'it has no subfolders-can remove
node.Remove
Else
RemoveUselessFolders(node)
End If
End If
Next
Catch ex As system.NullReferenceException
End Try
End Sub
This seems to throw NullReferenceException's and maybe deletes a folder
and maybe not. I just do not understand why this does not work. I do not
have VS, so I can not step through it and see what is (or isn't)
happening. Anyone have a clue?

If you need more info please let me know.

Thanks,
Jason

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.