Search code examples
vb.nettextboxmultilineopendialog

trying to output more than one filenam into textbox vb.net


Apologies if this is really simple but I'm fairly new to programming. I've created a program that uses an open dialog box and outputs the names of the file to a textbox.

Where I'm having issues is trying to get the textbox to display more than one line as all it seems to be doing is writing one line in the textbox.

The code I'm using is below, could someone please advise what I need to change so that I can get this to work.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click    
    Dim strFileName As String

    OpenFD.Multiselect = True
    OpenFD.InitialDirectory = "\\server\filename\"
    OpenFD.Title = "Open a Text File"
    OpenFD.Filter = "Text Files(.txt)|*.txt"
    Dim DidWork As Integer = OpenFD.ShowDialog()
    strFileName = OpenFD.FileName


    If DidWork = DialogResult.Cancel Then

        MsgBox("Cancel Button Clicked")

    Else

        strFileName = OpenFD.FileName
        TextBox1.Text = strFileName += 1

    End If
End Sub

I've managed to get everything else to work correctly but it's just this one thing.


Solution

  • Dim strFileName() As String
    
    '...
    
    Dim DidWork As Integer = OpenFD.ShowDialog()
    
    If DidWork = DialogResult.Cancel Then
        MsgBox("Cancel Button Clicked")
    Else
        strFileName = OpenFD.FileNames
        TextBox1.Multiline = True
        TextBox1.Text = ""
    
        For Each sFile as String in strFileName
            TextBox1.Text &= sFile & System.Enviroment.NewLine()
        Next
    End If