vb.net 选择多个文件并将文件名加载到“选中列表框”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32288216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:25:53  来源:igfitidea点击:

select multiple files and load the file names to "Checked List Box"

vb.netvisual-studio-2012

提问by Iwarai-kiwwata-iwarana_oi

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. :)

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

回答by Muhammad Saqib

Use InitialDirectoryproperty and a temporarily global string variable to remember your last open directory.

使用InitialDirectory属性和临时全局字符串变量来记住上次打开的目录。

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 LastDirin your program settings or in computer registry.

这将记住您在程序运行/活动时上次打开的目录。如果您希望对话框始终记住上次打开的目录存储LastDir在您的程序设置或计算机注册表中。

回答by Emmanouil Chountasis

I used a List(of String) in my test, you can change it so as to meet your needs.

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

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

我编辑了我的代码以使用一个对象。下面是一个例子。您可以使用它为 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

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