Thursday, January 3, 2013

Analyse your code in Visual Basic

Analyzing the needs

This is the best part. You need to analyze your code. Try to understand what the function needs to do what you want. This is not an easy step. With practice, you could build you brain to code directly in Visual Studio.

Lets only focus on this part of the code:


For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
      dirPath, FileIO.SearchOption.SearchTopLevelOnly, String_to_Search)

       oForm2.ListBox1.Items.Add(foundFile)

Next

For Each foundDirectory As String In My.Computer.FileSystem.GetDirectories( _ dirPath, FileIO.SearchOption.SearchTopLevelOnly)

      findfiles(foundDirectory) ''recursive search file : re-use "himself"

Next




We see that for every found file, it will inject the name inside the ListBox1. Simple right? Yes it is. Ok, What if we replace the ListBox1 for  TreeView ?



For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
      dirPath, FileIO.SearchOption.SearchTopLevelOnly, String_to_Search)

       'oForm2.ListBox1.Items.Add(foundFile)
       TreeView1.Nodes.Add(foundFile)

Next


For Each foundDirectory As String In My.Computer.FileSystem.GetDirectories( _ dirPath, FileIO.SearchOption.SearchTopLevelOnly)

      findfiles(foundDirectory) ''recursive search file : re-use "himself"

Next


Congratulations, your treeview will get all the file names on the root directory. Now, what do you have to do to put the files from the sub-directories? You know that the 2nd loop is capable to get the finds the files from all subdirectories but your recursive function will work.

What do you want?
I want the TreeView to display the files under his directory using a recursive function.

What do you have now?
I have the ListBox that gets all the files and directories injected using a recursive function.

What your TreeView need to give what you want?
Need a file path and an active node.
Need a file path and an active node

Is your current recursive function having all the parameter for the TreeView?
The TreeView need the active node to put the files Names. The Nodes is part of the structure of the TreeNodes.


Is not really easy to put in words what you have in mind. Some people will see another way to get to the results. Some solutions are more efficient while others are more appropriate.



Previous Page : Fill a TreeView from a Basic recursive search file <<< = = = =




Other posts: 







About 

I invite you to visit my blog for more articles and leave a comment. Check Technologies represents more than 10 years .... Computer and computer aided design.

Check Technologies ltée

235 Adrien-Provencher
Beloeil, (Québec), J3G 0C8, Canada
Tel : 514-705-7690
emal: info@checktechno.ca



No comments:

Post a Comment