【问题标题】:select multiple files and load the file names to "Checked List Box"选择多个文件并将文件名加载到“选中列表框”
【发布时间】:2015-11-24 03:05:09
【问题描述】:

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

【问题讨论】:

    标签: vb.net visual-studio-2012


    【解决方案1】:

    使用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
    

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

    【讨论】:

    • 谢谢 :) 它帮助了我。
    • @lwarai 这是我的荣幸 :)
    【解决方案2】:

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

    【讨论】:

    • 它给出了一个错误,而不是CheckedListBox1.AddRange,我猜你应该使用CheckedListBox1.Items.AddRange,但是在替换之后也会给出错误。 Overload resolution failed because no accessible 'AddRange' can be called without a narrowing conversion:
    • 正如我在测试中告诉您的那样,我创建了一个 List(Of String),是的,您拥有的 CheckListBox 是另一个对象,因此您需要将字符串转换为您 CheckListBox 使用的项目。
    • 我编辑了我的答案,以便您可以将其转换为您需要的对象。
    • 因为我是初学者,我不知道该怎么做。能否请您使用 CheckListBox 进行测试并纠正代码?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-10
    • 2017-01-16
    • 2012-03-20
    相关资源
    最近更新 更多