How to Write Data from a listbox to a text file

shepmom

Member
Joined
Aug 18, 2010
Messages
9
Programming Experience
Beginner
I am having trouble figuring out how to obtain data from a listbox and write it to a text file.

Here is my code:

Dim outputFile As StreamWriter
outputFile = File.CreateText(m_strDocumentName)
outputFile.Write(lstMontlyRainfall.Items)
outputFile.Close()

This is writing the following text to my textfile:

System.Windows.Forms.ListBox+ObjectCollection

How can I get the text out of the listbox for all items that were added?
 
Dim outputFile As StreamWriter
outputFile = File.CreateText(m_strDocumentName)
outputFile.Write(lstMontlyRainfall.Items)
outputFile.Close()

lstMontlyRainfall.items --> returns collection of listbox objects. It's not the same as their values. You need to use value of each item. :)

Try this code:

Dim outputFile As StreamWriter
outputFile = File.CreateText(m_strDocumentName)
dim i as integer = 0
while i < lstMontlyRainfall.Items.count
outputFile.Write(lstMontlyRainfall.Items(i).tostring)
i+=1
end while
outputFile.Close()
 
Back
Top