about copying info from a ".txt" file to a listbox

This myt seem lyk a very stupid question for most of you people out there, so i apologise for my ignorance. I am learning computing at school this year, and i had to do a project at school. Anyways, i am really really stuck on this, i have been trying 2 do it for the past 3 hours and it seems like i really need some help.

I have got this txt file with the info like this:

"DEF $13.45"
"ABC $12.95"
"Cheese Lovers $11.90"
"Vegie Supreme $11.90"
"$50.20"

I was wanting to copy the whole file into a listbox.

So this is what i tried, but it never seemed to work.

This is what i have thought of, i have tried alot of other methods but they dont work too.

FileOpen(2, "My Cart.txt", OpenMode.Input)
lstOut.Items.Add(2)

FileClose(2)

This code is not working as the character "2" keeps on getting added to the listbox.

I was also wondering whether or not i could get rid of the quote marks in my txt file when putting into the listbox. I would really appreciate it if sum1 would reply this question. As my project is handed in 3 days. I apologise again for my ignorance. THANK YOU GUYS:D



Answer this question

about copying info from a ".txt" file to a listbox

  • Alquimista09

    Hi Darenko,

    In note of your assistance, please set the thread as answered/ closed and credit your assistor - as further above.

    Happy you have it working.

    Thanks,

    Martin.


  • A.Russell

    I'm not exactly sure how fileopen works but i'd do it as follows

    Try
    Dim sr As StreamReader = New StreamReader("My Cart.txt")
    Dim line As String

    Do
    line = sr.ReadLine()
    lstout.Items.Add(line.Replace("""", ""))
    Loop Until line Is Nothing

    sr.Close()
    Catch ex As Exception
    msgbox(Ex.tostring)
    End Try


    HTH
    Andrew

  • Jim Perry

    Where you have a listbox on your form named ListBox1

    Dim lines As New System.Collections.Specialized.StringCollection

    Dim rdr As System.IO.TextReader = System.IO.File.OpenText("MyTextFile.txt")

    While rdr.Peek <> -1

    lines.Add(rdr.ReadLine.Replace("""", ""))

    End While

    rdr.Close()

    Me.ListBox1.DataSource = lines


  • DanBog

    A fuller example

    Dim FileDiag As New System.Windows.Forms.OpenFileDialog()

    ' Filter on text files

    FileDiag.Filter = "Text files (*.txt)|*.txt"

    ' The lines we are going to read

    Dim lines As New System.Collections.Specialized.StringCollection()

    Dim rslt As System.Windows.Forms.DialogResult = FileDiag.ShowDialog

    If rslt <> Windows.Forms.DialogResult.Cancel Then

    Dim rdr As System.IO.TextReader = System.IO.File.OpenText(FileDiag.FileName)

    While rdr.Peek <> -1

    lines.Add(rdr.ReadLine.Replace("""", ""))

    ' Alternate for not using collection. Requires listbox

    ' Use either or as you are mving a line on each loop

    ' Drop datasource setting below if using this!

    ' Me.ListBox1.Items.Add(rdr.ReadLine.Replace("""", ""))

    End While

    rdr.Close()

    ' Note: Listbox is required

    Me.ListBox1.DataSource = lines

    End If


  • RLovelett

    i got it working through codes as below

    Dim a As String
    FileOpen(2, "My Cart.txt", OpenMode.Input)
    Do While Not EOF(2)
    Input(2, a)
    lstOut.Items.Add(a)
    Loop
    FileClose(2)

    I dont really understand it, but my teacher tought me it and it works so everythings fine... thanks alot for yous reply!! very helpful this forum is :D cheeerz


  • stswordman

    Thats fine so am I. However, It was the solution from BeanR that you used and he should be credited! I couldn't see his Username when responding to you.

    It is important to people, I am not trying to high and mighty.


  • Paul A Morrison

    as far as I am aware of, VB.NET has no methods like FileOpen, unless it's a function in your project

    you need to know how to open and read a file using either a StreamReader (easier) or a FileStream:

    http://msdn2.microsoft.com/en-us/library/system.io.streamreader.aspx

    http://msdn2.microsoft.com/en-us/library/system.io.filestream.aspx

    How exactly do you want to add items to a listbox Just one row of data in the file per entry on the listbox



  • Terrence Chan

    sorry im new here, not sure how things work... apologise greatly:S
  • about copying info from a ".txt" file to a listbox