What is the best way to display directory folders on a window form?

dominico

Well-known member
Joined
Mar 9, 2005
Messages
57
Programming Experience
3-5
For instance, I have a mapped network drive pointing to directory "A" on the server.

Under directory "A", there are 5 different directory folders starting from "B1" to "B5".

Under each of these "B" folders, there is a directory folder called "C".

What I need to ascomplish is to be able to access the files under the "C" folder in each of the "B" folders.


So far, I have created a combobox to display the file names under the "C" folder of each of the "B" folders. I would like all the files under the "C" folders to display on the dropdown list. I know that I can hardcode the directories and filenames into the combobox. However, it seems to be cumbersome this way because I would have to hardcode a bunch of mapped drives pointing to each and every file under the "C" folders. This seems like the only way I can think of so far.


I wonder if there is a better way to do this.


I do need your expertise on how we can ascomplish this complex task. I am at my ends wit. I have been thinking about different approaches for the past two days but still couldn't come up with the best solution for it.

Please advise gentlemen. I thank everyone in advance.


dominico
 
Last edited:
You can call IO.Directory.GetDirectories to get an array of fully qualified folder paths that are the subfolders of the specified folder. You can iterate over the array and call IO.Path.GetFileName to get just the folder name itself for each path and assign it back to the array element. You can then bind the array to your ComboBox.
VB.NET:
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each folderPath As String In IO.Directory.GetDirectories("C:\")
            MessageBox.Show(IO.Path.GetFileName(folderPath))
        Next
End Sub
Note that you call GetFileName and not GetFolderName. If the path is a file, GetFileName returns the name of the file and GetFolderName returns the name of the folder it is in. If the path is a folder, GetFileName returns the name of the folder and GetFolderName returns the name of the parent folder.

I'm not sure if this would be suitable in your case but you might want to think about using a TreeView instead of ComboBoxes. Windows Explorer uses a TreeView for folders, as does the FolderBrowserDialog.
 
jmcilhinney said:
You can call IO.Directory.GetDirectories to get an array of fully qualified folder paths that are the subfolders of the specified folder. You can iterate over the array and call IO.Path.GetFileName to get just the folder name itself for each path and assign it back to the array element. You can then bind the array to your ComboBox.
VB.NET:
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each folderPath As String In IO.Directory.GetDirectories("C:\")
MessageBox.Show(IO.Path.GetFileName(folderPath))
Next
End Sub
Note that you call GetFileName and not GetFolderName. If the path is a file, GetFileName returns the name of the file and GetFolderName returns the name of the folder it is in. If the path is a folder, GetFileName returns the name of the folder and GetFolderName returns the name of the parent folder.

I'm not sure if this would be suitable in your case but you might want to think about using a TreeView instead of ComboBoxes. Windows Explorer uses a TreeView for folders, as does the FolderBrowserDialog.


Hi jmcilhinney buddy :)

Thanks for the reply. I went ahead and did it with using the combobox. It is okay. I am not good with array stuff so I try not to kill myself with them unless I have no choice.

I do have another issue now. After I got the combobox to list the files, I created another modeless form to display the content of the selected file on the combobox list. However, I don't know how to create an "Addhandler even" so that whenever a file is selected on the combobox, it will trigger my displayformbutton_click even to pop up the form to display the content of that file. Could you please show me how to do that ? I read the msdn examples but still got no idea what they are talking about. It is so confusing. Please help me out. If you want me to open a new case for this then I will be happy to acomodate you.

dominico
 
You should only be using AddHandler for controls created at runtime or if you need to change the event handler for some reason. If the control exists at design-time and you know it needs an event handler then do it add it at design-time. Double-click the control to create a handler for the default event or use the drop-down lists at the top of the code window for other events.
 
jmcilhinney said:
You should only be using AddHandler for controls created at runtime or if you need to change the event handler for some reason. If the control exists at design-time and you know it needs an event handler then do it add it at design-time. Double-click the control to create a handler for the default event or use the drop-down lists at the top of the code window for other events.

Thanks. I took your advice. It works now. I added the combobox textchanged even to the displaybuttonclick even.
 
Back
Top