Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Listing Directories in comboBox

How can i list all the directories in combox
I mean if i had foder /Directory (Main) that contains three other directories i.e. dir1 , dir2 , dir3
& combo box should display the dir1,dir2,dir3 as its items so that choosing the particular directory all the files in that directory are displayed in listbox.[i dont want to use drvecombobox coz combobox that picks the folders as item in it are catagories].Please provde code with help . Any help is welcomed
[463 byte] By [DEVILSAN] at [2007-11-11 7:38:01]
# 1 Re: Listing Directories in comboBox
Here's the minimum amount of code required:

cboBox.Items.AddRange(System.IO.Directory.GetDirectories("D:\Directory"))

Alternatively, you may loop through the array returned by GetDirectories and add each item to the combobox's Items collection.
Phil Weber at 2007-11-11 21:48:51 >
# 2 Re: Listing Directories in comboBox
works with ComboBox1.Items.AddRange(System.IO.Directory.GetDirectories("D:\"))
but not with
ComboBox1.Items.AddRange(System.IO.Directory.GetDirectories(Environment.CurrentDirectory))
DEVILSAN at 2007-11-11 21:49:46 >
# 3 Re: Listing Directories in comboBox
What is the value of Environment.CurrentDirectory?
Phil Weber at 2007-11-11 21:50:51 >
# 4 Re: Listing Directories in comboBox
value of Environment.CurrentDirectory

D:\My Works\Windows Appl\WindowsApplication2\bin

but still it doesnt get
i did it by
textbox1.text=Environment.CurrentDirectory
DEVILSAN at 2007-11-11 21:51:48 >
# 5 Re: Listing Directories in comboBox
Are there any subdirectories under that folder? It works fine here.
Phil Weber at 2007-11-11 21:52:53 >
# 6 Re: Listing Directories in comboBox
Oooooooooops their wasnt any folder .lol i just created & got it

As i wanted to display only the directory names io did this:
For i As Integer = 0 To folders.GetUpperBound(0) Step 1
'Remove the path.
folders(i) = IO.Path.GetFileName(folders(i))
Next i

now i got the directory names as combobox items
Actually I was building an aplicatin in which we can create a directory at runtime by entering the directory name

i wrote Application.StartupPath & "\" & & TextBox1.text

but while the directory is created the combo box which is on form1 does not displays it

& moreover if the directory already exists it gives Path/File access error
DEVILSAN at 2007-11-11 21:53:52 >
# 7 Re: Listing Directories in comboBox
I am doing a similar project, and am having problems once I get the combobox populated with the data from a text file, you should be able to select one of the entries and it populate 3 textboxs. but I am having a problem with that part because no matter which item i select from the combobox it always displays the last entry.

here is the code that gets the information from the file.

Dim FileNum As Integer
FileNum = FreeFile()
FileOpen(FileNum, "f:\project2\project2.txt", OpenMode.Random, OpenAccess.Read, OpenShare.Shared, Len(udtProject2))
cmbUpDate.Text = "Select Record..."
Do
FileGet(FileNum, udtProject2)
cmbUpDate.Items.Add(udtProject2.strDirectory)
Loop Until EOF(FileNum)
FileClose(FileNum)

then in the combobox all i have for it is:

txtDirectoryName.Text = udtProject2.strDirectory
txtProjectPrefix.Text = udtProject2.strSubDirectory
txtProjectNumber.Text = udtProject2.strNumberofSub

but I think my problem is when I am getting the information.
SWI03 at 2007-11-11 21:54:51 >
# 8 Re: Listing Directories in comboBox
You're setting the textboxes to the value of udtProject2, which is the last record you read from the text file. I recommend adding a .ToString function to your UDT that returns strDirectory:

Public Overrides Function ToString() As String
Return strDirectory
End Function

Then in your loop, add the entire UDT to the combobox:

cmbUpDate.Items.Add(udtProject2)

Finally, in the combo's SelectedIndexChanged event, cast the SelectedItem to a Project2 and set the textbox values:

Private Sub cmbUpDate_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles cmbUpDate.SelectedIndexChanged
Dim SelectedProject As Project2 = DirectCast(cmbUpDate.SelectedItem, Project2)
With SelectedProject
txtDirectoryName.Text = .strDirectory
txtProjectPrefix.Text = .strSubDirectory
txtProjectNumber.Text = .strNumberofSub
End With
End Sub
Phil Weber at 2007-11-11 21:56:00 >