选择多个文件并将文件名加载到“Checked List Box”(select multiple files and load the file names to “Checked List Box”

编程入门 行业动态 更新时间:2024-10-25 07:27:30
选择多个文件并将文件名加载到“Checked List Box”(select multiple files and load the file names to “Checked List Box”)

我需要:单击一个按钮时打开文件对话框,然后我将选择多个文件,然后单击确定,然后文件将出现在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 If

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

最满意答案

我在测试中使用了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 Class

编辑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

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

更多推荐

本文发布于:2023-08-02 14:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1376922.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   并将   文件名   加载   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!