You're not too far off. The following would work
Const folderName As String = "c:\temp"
Const searchPattern As String = "*.txt"
Dim fileNames As String() = Directory.GetFiles(folderName, searchPattern)
For i As Long = 0 To fileNames.GetUpperBound(0)
createtxtbox(fileNames(i), i)
Next
Then the sub-routine could look something like this
Private Sub createtxtbox(ByVal fileName As String, ByVal i As Long)
Dim newTxtBox As TextBox = New TextBox()
newTxtBox.Name = i.ToString()
newTxtBox.Multiline = True
newTxtBox.ScrollBars = ScrollBars.Both
'You'll need to position the text box in some way - I've used the file "number"
newTxtBox.Top = (i * 20) + 1
newTxtBox.Left = (i * 20) + 1
newTxtBox.Text = File.ReadAllText(fileName)
Me.Controls.Add(newTxtBox)
End Sub
You should include some try-catch exception checking and whatever your formatting requirements are.
Note I used
ReadAllText
just to get the entire file as a single string, if you want to get it line-by-line use
ReadAllLines
instead and loop through the contents with a
For Each
or
For
loop