Question:
Visual Basic Saving a list to a text file. Please Help Me?
Charles X
2007-05-06 16:54:07 UTC
Please help me with this. I have searched and searched the internet and can not figure out how to do this. I need to save the contents of a list box to a text file that can later be loaded back into the list. Please Please Please if at all possible give me an example of the code I need to do this. I am a horrible programmer (if you can call me that) but i have to take this class for a unrelated major. Thanks
Four answers:
Bohdan
2007-05-06 17:14:45 UTC
'declaration

Dim w As IO.StreamWriter

Dim r As IO.StreamReader





this function saves the values to the file:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim i As Integer

w = New IO.StreamWriter("c:\test.txt")

For i = 0 To ListBox1.Items.Count - 1

w.WriteLine(ListBox1.Items.Item(i))

Next

w.Close()

End Sub



And this procedure opens the file and poopulates the listbox:



Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

r = New IO.StreamReader("c:\test.txt")

While (r.Peek() > -1)

ListBox2.Items.Add(r.ReadLine)

End While

r.Close()

End Sub
Shaifu
2007-05-06 18:02:48 UTC
Option Explicit

Private Sub Command1_Click()

' Call the Save List Routine

Call SaveList

End Sub

Private Sub Form_Load()

Dim intCtr As Integer

' Populate the listbox with test

' data

For intCtr = 0 To 10

With List1

.AddItem "Item " & intCtr

End With

Next intCtr

End Sub



Private Sub SaveList()

Dim iHandle As Integer

' Handle to the next available file

Dim iCtr As Integer

' Loop Counter

' get the next available file

iHandle = FreeFile()

' Open the file for output.

' Note that if the file does not exist then it will

' be created automatically

Open "C:\List Contents.txt"

For Output As iHandle

For iCtr = 0 To List1.ListCount

' Iterate over each item in the list

' and write the value to the file

Print #iHandle, List1.List(iCtr)

Next iCtr

Close iHandle

End Sub
anonymous
2007-05-06 17:54:22 UTC
The previous suggestion of a StreamReader and StreamWriter is good, but I would use an XMLWriter, since you can bind a listbox to an XML file quite easily.



http://msdn2.microsoft.com/en-us/library/system.xml.xmlwriter(vs.71).aspx



http://msdn2.microsoft.com/en-us/library/system.windows.forms.listcontrol.datasource.aspx
mershon
2016-10-15 03:00:53 UTC
you may create a textual content cloth document and whilst the person searches utilising the hunt bar you may open the textual content cloth document in append mode and upload the hunt term to the history document. if so each and every seek made would be extra to the hsitory.txt. as quickly as whilst the person clicks on the history button, you may then open the history.txt document utilising the examine mode and exhibit the contents utilising a textual content cloth container on that style. you may prefer to apply the filestream or such references to try this.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...