Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opposite of SaveTextToFile?

Status
Not open for further replies.
Joined
Aug 17, 2008
Messages
28
I found some code that helped me save text to a file, and tweaked it to work with listboxes.

What I want to know now is, how do I get this text BACK into the listbox?

The code is here: - I used a loop to save from a listbox. Will post code later, as I am on a different PC just now.

GeodesicDragon
curquhart.co.uk
 

Code:
Dim strTextLine As String

Open "C:\SomeTextFile.txt" For Input As #1
listbox1.Clear
Do While Not EOF(1)   [green]' Loop until end of file.[/green]
   Line Input #1, strTextLine   [green]' Read line into variable.[/green]
   listbox1.AddItem strTextLine    [green]' Add line to listbox[/green]
Loop
Close #1

Have fun.

---- Andy
 
Whenever it saves, it always adds a blank line to the start of the text file. How do I stop it doing that?
 

Well, the code I gave you takes text from text file and puts it into a listbox. If you have any blank lines in your text file - some other piece of code is doing it.

Show me code that writes text to a text file and let's see what's going on.

Have fun.

---- Andy
 
Here is the SaveTextToFile Function:

Code:
ublic Function SaveTextToFile(FileFullPath As String, _
 sText As String, Optional Overwrite As Boolean = False) As _
 Boolean
 
On Error GoTo ErrorHandler
Dim iFileNumber As Integer
iFileNumber = FreeFile

If Overwrite Then
    Open FileFullPath For Output As #iFileNumber
Else
    Open FileFullPath For Append As #iFileNumber
End If

Print #iFileNumber, sText
SaveTextToFile = True

ErrorHandler:
Close #iFileNumber
End Function

And here is how I use it to save the listbox contents to a file:

Code:
For i = 0 To lstListbox.ListCount - 1
    SaveTextToFile "C:\Contents.txt", lstListbox.List(i - 1)
Next i
 
lstListbox.List(i - 1)

The -1 will write a blank line at the beginning of the file and probably not write the last line.


[gray]Experience is something you don't get until just after you need it.[/gray]
 
Thanks for that, guys. It works now.

All I need to do now is find out how to check if text in a listbox already exists.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top