Reputation: 13
I need to: open the file dialog box when a button clicked, and then I'll select multiple files, then click ok, then the files will appear on Checkedlistbox1. And I want the dialog box to remember the folder path which I browsed last (so when I click that button again, it will take me to that location). But when I just run "openfiledialog" I can't select multiple files, and when I add further code, the program gives errors. Please shed some light here. :)
Dim fbd As New OpenFileDialog With { _
.Title = "Select multiple files", _
.FileName = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)}
If fbd.ShowDialog = DialogResult.OK Then
CheckedListBox1.Items.AddRange(Array.ConvertAll(IO.Directory.GetFiles(fbd.FileNames), Function(f) IO.Path.GetFileName(f)))
End If
Upvotes: 1
Views: 12700
Reputation: 590
I used a List(of String) in my test, you can change it so as to meet your needs.
Dim fbd As New OpenFileDialog With { _
.Title = "Select multiple files", _
.Multiselect = True, _
.FileName = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)}
If fbd.ShowDialog = DialogResult.OK Then
CheckedListBox1.AddRange(fbd.FileNames.Select(Function(f) IO.Path.GetFileName(f)))
End If
EDIT
I edited my code so as to use an object. The following is an example. You can use it to create the needed object for CheckBoxList
Public Property CheckedListBox1 As New List(Of TestClass)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fbd As New OpenFileDialog With { _
.Title = "Select multiple files", _
.Multiselect = True, _
.FileName = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)}
If fbd.ShowDialog = DialogResult.OK Then
CheckedListBox1.AddRange(fbd.FileNames.Select(Function(f) New TestClass With {.Name = IO.Path.GetFileName(f)}))
End If
End Sub
Public Class TestClass
Public Property Name As String
End Class
EDIT 2
Dim fbd As New OpenFileDialog With { _
.Title = "Select multiple files", _
.Multiselect = True, _
.FileName = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)}
If fbd.ShowDialog = DialogResult.OK Then
CheckedListBox1.Items.AddRange(fbd.FileNames.Select(Function(f) IO.Path.GetFileName(f)).ToArray)
End If
Upvotes: 2
Reputation: 2273
Use InitialDirectory
property and a temporarily global string variable to remember your last open directory.
Dim LastDir As String = ""
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim fbd As New OpenFileDialog
If LastDir = "" Then Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
With fbd
.Title = "Select multiple files" ' Title of your dialog box
.InitialDirectory = LastDir ' Directory appear when you open your dialog.
.Multiselect = True 'allow user to select multiple items (files)
End With
If fbd.ShowDialog() = Windows.Forms.DialogResult.OK Then ' check user click ok or cancel
LastDir = Path.GetDirectoryName(fbd.FileName) 'Update your last Directory.
' do your stuf here i.e add selected files to listbox etc.
For Each mFile As String In fbd.FileNames
CheckedListBox1.Items.Add(mFile, True)
Next
End If
this will remember your last open directory while your program is running/alive.
If you want your dialog box always remember last opened directory store LastDir
in your program settings or in computer registry.
Upvotes: 1