我需要:单击一个按钮时打开文件对话框,然后我将选择多个文件,然后单击确定,然后文件将出现在Checkedlistbox1上。 我希望对话框能够记住我最后浏览的文件夹路径(所以当我再次单击该按钮时,它会将我带到该位置)。 但是当我运行“openfiledialog”时,我无法选择多个文件,当我添加更多代码时,程序会出错。 请在这里说清楚。 :)
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 IfI 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最满意答案
我在测试中使用了List(of String),您可以对其进行更改以满足您的需求。
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编辑
我编辑了我的代码以便使用一个对象。 以下是一个例子。 您可以使用它为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 ClassDim 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编辑2
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 IfEDIT
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 ClassDim 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 IfEDIT 2
更多推荐
发布评论