1 Replies - 6599 Views - Last Post: 22 May 2008 - 09:27 PM Rate Topic: -----

#1 JMDizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-May 08

Populating Listbox from textfile

Posted 22 May 2008 - 08:37 PM

Im trying to Populate a listbox based on a text file.
I have my listbox on my form. I want to be able to save whatever is in the collection to a .txt file. Then re-open it later in inside the program, and have my collection remain the same.

For example:
my llistbox collection as 3 items in it.

Apples
Grapes
Pears

I would like it to save to the text file.then when i open it again, the listbox would take each line and set it as a item in the list box.

so it would read the 1st line of text in teh txt file, and set that as the 1st item.
then it would take the 2nd line of text from the text file and set the 2nd item in the listbox.
and so on.

My attempt:

  Sub Savedocument()
		Dim outputfile As System.IO.StreamWriter

		outputfile = System.IO.File.CreateText(documentname)
		outputfile.Write(lstStatuslist.Items)
		outputfile.Close()
		isChanged = False
	End Sub


	If ofdOpen.ShowDialog = DialogResult.OK Then
			documentname = ofdOpen.FileName
			inputfile = System.IO.File.OpenText(documentname)
			lstStatuslist.Text = inputfile.ReadToEnd
			inputfile.Close()
	End If




Please help me out. As you can probally see im lost =)

This post has been edited by JMDizzle: 22 May 2008 - 08:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Populating Listbox from textfile

#2 Ändrew  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 25
  • View blog
  • Posts: 312
  • Joined: 21-April 08

Re: Populating Listbox from textfile

Posted 22 May 2008 - 09:27 PM

Try this for opening and saving. Used it in my program.
Open
Dim Open As New OpenFileDialog()
		Dim myStreamReader As System.IO.StreamReader
		Open.Filter = "Quake Engine Config (*.txt)|*.txt|All files (*.*)|*.*"
		Open.CheckFileExists = True
		Open.Title = "Open Whatever!"
		Open.ShowDialog(Me)
		Try
			Open.OpenFile()
			myStreamReader = System.IO.File.OpenText(Open.FileName)
			ListBox1.Text = myStreamReader.ReadToEnd()
		Catch ex As Exception
			' Do nothing on Exception
		End Try


Save
		Dim Save As New SaveFileDialog()
		Dim myStreamReader As System.IO.StreamReader
		Save.Filter = "Quake Engine Config (*.txt)|*.txt|All files (*.*)|*.*"
		Save.CheckFileExists = True
		Save.Title = "Save Whatever!"
		Save.ShowDialog(Me)
		Try
			Save.OpenFile()
			myStreamReader = System.IO.File.OpenText(Save.FileName)
			ListBox1.Text = myStreamReader.ReadToEnd()
		Catch ex As Exception
			' Do nothing on Exception
		End Try


Try that.

Andrew
Was This Post Helpful? 1

Page 1 of 1