Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FolderBrowserDialog to listbox 1

Status
Not open for further replies.

dan

MIS
Oct 7, 1998
298
US
I have chosen a directory using the FolderBrowerDialog but can't figure out how to display the file list in a listbox.
Code:
FolderBrowserDialog1.ShowDialog()
txtDir.Text = FolderBrowserDialog1.SelectedPath

Thanks for any suggestions.
Dan
 
What you are asking doesn't quite make sense. Are you saying that each time you select a directory you want to add that to a list box? Because a FBD doesn't return a list of anything of there is no "file list" to return. Or do you mean you want to split out the path into each of its folders?

Dim fldrs as string() = FolderBrowserDialog1.SelectedPath.Split("\")

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Or did you mean an OpenFileDialog box?

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I want to display a list of the files in the directory in a listbox.
Dan
 
Code:
        Dim GetFiles As New OpenFileDialog
        GetFiles.Multiselect = True

        If GetFiles.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim FullFileNames As String() = GetFiles.FileNames
            Dim JustFileNames As String() = GetFiles.SafeFileNames

            ListBox1.Items.AddRange(FullFileNames) '<- or JustFileNames depending on what you want.
        End If

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Or if you know the directory

Code:
Dim AllFiles As String() = IO.Directory.GetFiles("DirectoryPath")

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I don't want an OpenFileDialog box, just pick the folder and display the list of files in that folder:

FolderBrowserDialog1.ShowDialog()
txtDir.Text = FolderBrowserDialog1.SelectedPath
Me.Refresh()
Dim GetFiles As New OpenFileDialog??
GetFiles.Multiselect = True
If GetFiles.ShowDialog = 'here I only want FolderBrowserDialog

Windows.Forms.DialogResult.OK Then
Dim JustFileNames As String() = GetFiles.SafeFileNames
ListBox1.Items.AddRange(JustFileNames)
End If
 
While I still didn't get what you were saying, between what you wrote and I wrote all of the pieces are here in this thread. I really hope this doesn't seem rude, but you might be jumping the gun a bit if I have to type it out. We might have needed to add the error checking that I do here, but the rest is all right here.

Code:
        Dim fdb As New FolderBrowserDialog

        If fdb.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim FullFileNames As String() = Directory.GetFiles(fdb.SelectedPath)

            If FullFileNames.Length > 0 Then
                ListBox1.Items.AddRange(FullFileNames)
            End If
        End If
or to use your FolderBrowserDialog1 that seems is alreay in place.
Code:
        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim FullFileNames As String() = Directory.GetFiles(FolderBrowserDialog1.SelectedPath)

            If FullFileNames.Length > 0 Then
                ListBox1.Items.AddRange(FullFileNames)
            End If
        End If

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Error prevention might have been a better word then error checking?

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Sorwen-
Thanks for your help. This is what I am looking for, except I don't want the path in the filenames (short filenames only):
If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim JustFileNames As String() = Directory.GetFiles(FolderBrowserDialog1.SelectedPath)
txtDir.Text = FolderBrowserDialog1.SelectedPath
Me.Refresh()
If JustFileNames.Length > 0 Then
ListBox1.Items.AddRange(JustFileNames)
End If
End If
End Sub

dan
 
Then you will need to loop through each one and use IO.Path. So replace ListBox1.Items.AddRange(JustFileNames) with something like this:
Code:
        For Each strName As String In JustFileNames
            Dim JustName As String = IO.Path.GetFileName(strName) 'or use IO.Path.GetFileNameWithoutExtension(strName)
            ListBox1.Items.Add(JustName)
        Next

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Sorwen-
Thanks so much for your help. This is exactly what I needed. (I'm not sure how this is easier than VB6 dirlistbox (-: )
 
np. glad it worked out.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top