Reputation: 429
I have an issue that I would like some help with.
I am trying to open a file dialog, select text file, and then display text in listbox.
I have the following code. It opens dialog, but won't display text in listbox.
Any suggestions?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImportKeywordList.Click
Dim oReader As StreamReader
OpenFileDialog1.CheckFileExists = True
OpenFileDialog1.CheckPathExists = True
OpenFileDialog1.DefaultExt = "txt"
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
OpenFileDialog1.Multiselect = False
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
oReader = New StreamReader(OpenFileDialog1.FileName, True)
ListBox1.Text = oReader.ReadToEnd()
End If
End Sub
Upvotes: 0
Views: 151
Reputation: 216243
Listbox display text through the Items collection not through the Text property. In a ListBox the Text property represent the text of the currently selected item
An example could be written in this way
....
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Using oReader = New StreamReader(OpenFileDialog1.FileName, True)
While oReader.Peek <> -1
ListBox1.Items.Add(oReader.ReadLine())
End While
End Using
End If
....
Upvotes: 1