HELP! loading file in listbox!

I am trying to load a text file in my list box. I wrote the following code. But it doesn't work. The text file name is membersphone.txt. I need help in VB NET. How do I get the data from the text file loaded in the listbox? Here's the code:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
Dim sw As IO.StreamWriter = IO.File.AppendText("MEMBERSPHONE.txt")
For i = 0 To lstBox.Items.Count - 1
sw.WriteLine(lstBox.Items(i))
Next
sw.Close()
End Sub


Comments

  • : I am trying to load a text file in my list box. I wrote the following code. But it doesn't work. The text file name is membersphone.txt. I need help in VB NET. How do I get the data from the text file loaded in the listbox? Here's the code:
    :
    : Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    : Dim i As Integer
    : Dim sw As IO.StreamWriter = IO.File.AppendText("MEMBERSPHONE.txt")
    : For i = 0 To lstBox.Items.Count - 1
    : sw.WriteLine(lstBox.Items(i))
    : Next
    : sw.Close()
    : End Sub
    :
    :

    I havnt yet tried text file stuff in VB.NET, but the above code looks like its writing the contents of the list box TO the file, not the other way around as your question states. Is that what you want???

  • : : I am trying to load a text file in my list box. I wrote the following code. But it doesn't work. The text file name is membersphone.txt. I need help in VB NET. How do I get the data from the text file loaded in the listbox? Here's the code:
    : :
    : : Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    : : Dim i As Integer
    : : Dim sw As IO.StreamWriter = IO.File.AppendText("MEMBERSPHONE.txt")
    : : For i = 0 To lstBox.Items.Count - 1
    : : sw.WriteLine(lstBox.Items(i))
    : : Next
    : : sw.Close()
    : : End Sub
    : :
    : :
    :
    : I havnt yet tried text file stuff in VB.NET, but the above code looks like its writing the contents of the list box TO the file, not the other way around as your question states. Is that what you want???
    :
    :

    You ned to use a Stream Reader to read from the text file. The code will look something like this ...

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim i As Integer
    Dim sr As New IO.StreamReader("MEMBERSPHONE.txt")

    For i = 0 To lstBox.Items.Count - 1
    lstBox.Items.add(sr.ReadLine())
    Next
    sr.Close()
    End Sub





Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories