简体   繁体   中英

How can I get my text file data to display in listbox?

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

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
....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM