473,383 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Newb needs a little help

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
3 1090
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hari | last post by:
Basically I would like to downlod the visual basic 6.0 compiler, but i already have the vb.net compiler. I had to pay for the VB.net IDE, just wondering if I can get the vb 6.0 IDE for free or not....
2
by: Alex | last post by:
Basically I want to parse some C++ source code and replace some constants. I've mangaed to read in the file and replace some of the things that need replacing. but I'm having a hard time...
1
by: Bond | last post by:
Hi! There is a section on my webpage that needs some formatting done. Currently I'm displaying some information at the top of the page using <pre class="....">...</pre> I have this working to the...
5
by: Mitch | last post by:
Built a basic word game for the kids, using if, else , string input, etc, its kinda cool I would like to put in some bmp or jpg to color it up a bit. The books i have do not show how to display a...
24
by: Apotheosis | last post by:
The problem professor gave us is: Write a program which reads two integer values. If the first is less than the second, print the message "up". If the second is less than the first, print the...
11
by: The_Kingpin | last post by:
Hi all, I'm new to C programming and looking for some help. I have a homework project to do and could use every tips, advises, code sample and references I can get. Here's what I need to do....
29
by: jaysherby | last post by:
I'm new at Python and I need a little advice. Part of the script I'm trying to write needs to be aware of all the files of a certain extension in the script's path and all sub-directories. Can...
12
by: joaotsetsemoita | last post by:
Hello everyone, im completly new to vb.net and I was assigned to do a simple piece of software that just had to select from um db in a MS access data base and insert into a SQL server Database....
4
by: Cyprus106 | last post by:
I realize how I'm going about this is somewhat odd. I have to construct a program for windows mobile that only sends a couple queries to a remote MySQL server and hopefully gets some success/fail...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.