使用OpenFileDialog显示选定的多个文件时出现问题

时间:2013-08-04 20:45:42

标签: vb.net openfiledialog

我在使用OpenFileDialog类显示多个文件名时遇到问题。 我已经选择Multiselect设置为true,并且可以在DialogBox中选择多个文件,但只在TextBox1区域中打印一个文件。

这是我的代码:

Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    OpenFileDialog1.ShowDialog()
End Sub

Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
    ' Dialog Result
    Dim FilesSelected As DialogResult
    ' Are Files Selected?
    If (FilesSelected = System.Windows.Forms.DialogResult.OK) Then
        ' If Files Identify Array
        Dim file() As String
        file = OpenFileDialog1.FileNames
        For Each file In OpenFileDialog1.FileNames
            TextBox1.Text = (CurDir() & "")
        Next
    End If

End Sub
End Class

2 个答案:

答案 0 :(得分:0)

每次在foreach循环中都会覆盖Text。

答案 1 :(得分:0)

首先,确保将TextBox的 MultiLine 属性设置为true,并将其调整为具有可接受的高度。同时将 ScrollBars 属性设置为Vertical

然后你的代码可以用这种方式编写(没有显式循环)

Dim txt = string.Join(Environment.NewLine, OpenFileDialog1.FileNames)
TextBox1.Text = txt