2

我有下面的代码查找组合框中显示的值,然后使用与所选扩展名相关的所选文件名填充列表框(也许代码会更有意义!)

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles    ComboBox1.SelectedIndexChanged

    Dim lyxfle As New IO.DirectoryInfo(sPath)
    Dim diar1 As IO.FileInfo() = lyxfle.GetFiles()
    Dim MyExt As String = Nothing

    Dim MyVariable As String

    Dim sFile As String

    MyVariable = ComboBox1.Text

    If MyVariable = "Forms" Then MyExt = "*.pff"
    If MyVariable = "Reports" Then MyExt = "*.mdb"
    If MyVariable = "Spreadsheets" Then MyExt = "*.xlsm"
    ListBox2.Items.Clear()

    sFile = Dir$(sPath & MyExt)

    Do While CBool(Len(sFile))
        ListBox2.Items.Add(System.IO.Path.GetFileNameWithoutExtension(sFile))

        sFile = Dir$()
    Loop
End Sub

以下是我正在努力解决的问题

If MyVariable = "Spreadsheets" Then MyExt = "*.xlsm"

我基本上还需要它来查看扩展名 .xls 和 .xlsx 并在列表中包含所有文件

Path 和 sPath 都在类的顶部声明为私有字符串

任何建议

4

1 回答 1

1

我会尽量保持简单,然后使用你所拥有的。此代码将从文件夹中获取所有文件。然后它将与您的 MyExt 选择相匹配。如您所见,我添加了 excel 文件扩展名,它们之间用逗号分隔(实际上可以是任何特殊字符)。然后我要做的就是查看 MyExt 是否包含 FileExtension 并将其添加到列表框中:

    Dim MyExt As String = Nothing

    Dim MyVariable As String

    Dim sFile As String

    MyVariable = ComboBox1.Text

    If MyVariable = "Forms" Then MyExt = "*.pff"
    If MyVariable = "Reports" Then MyExt = "*.mdb"
    If MyVariable = "Spreadsheets" Then MyExt = ".xlsm,.xls"

    Dim lyxfle As New IO.DirectoryInfo(sPath)
    Dim diar1 As IO.FileInfo() = lyxfle.GetFiles()

    For Each file As IO.FileInfo In diar1

        If MyExt.Contains(file.Extension) Then

            ListBox2.Items.Add(IO.Path.GetFileNameWithoutExtension(file.Name))
        End If

    Next
于 2013-10-16T13:41:14.453 回答