I have a text file in my desktop...
In this file we have five lines:
Line 1
Line 2
Line 3
Line 4
Line 5
If the user choose this tex
Private Sub OpenList()
Dim openfile = New OpenFileDialog()
openfile.Filter = "Text (*.txt)|*.txt"
If (openfile.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
Dim myfile As String = openfile.FileName
Dim allLines As String() = File.ReadAllLines(myfile)
For Each line As String In allLines
listBox.Items.Add(line)
Next
End If
End Sub
Try this:
Dim lines() As String = IO.File.ReadAllLines("C:\dir\file.txt")
ListBox1.Items.AddRange(lines)
More information is at MSDN.
Use:
Dim openfile = New OpenFileDialog() With {.Filter = "Text (*.Text)|*.txt"}
If (openfile.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
For Each line As String In File.ReadAllLines(openfile.FileName)
ListBox1.Items.Add(line)
Next
End If
Code is a lot better.