Reading lines of text

RichColeUK
01-09-2003, 10:12 AM
ive found out how to read a line of text from a text file, how do i move down the text file one line at a time after each click of a mouse.

I am currently using

line input #1, string

this will only display the first line of text

FulleS
01-09-2003, 10:16 AM
If you issue the same command again it will return the next line of text.

SoLoGHoST
01-09-2003, 10:47 AM
Ok, here is an example that works fine. First, make sure you have the Microsoft Scripting Runtime added in your References.... Create a Textbox and a CommandButton on a form. Leave the default names (Text1 and Command1) and add this code:


Option Explicit
Dim oWCC As New Scripting.FileSystemObject
Dim file As Scripting.TextStream

Private Sub Command1_Click()
On Error GoTo EndOfFile
Text1.Text = file.ReadLine
DoEvents
Exit Sub

EndOfFile:
MsgBox "There is no more text inside of this .txt file to read from!", vbOKOnly, "All Done!"
Text1.Text = ""

Exit Sub
End Sub

Private Sub Form_Load()
Set file = oWCC.OpenTextFile(App.Path & "\text.txt")
End Sub


Make sure to change App.Path & "\text.txt" to the actual .txt file you are opening. And if you are going to be using multiple files or you will only be needing this file once, make sure to close it by adding file.close after Text1.Text = "", this will make it so that the file will no longer be able to be read from, unless you set a new file. I set the file in the Form_Load Event, but it can be done anywhere. As long as it's set before you do any reading from it.

Hope this helps ya some.

Squirm
01-09-2003, 10:51 AM
There is absolutely no need to use the FSO here. Native VB file IO methods can be used just as easily, work faster than the FSO, and not cause an extra dependancy to be added to the project. The FSO was designed for use in scripting, and should be avoided in VB when it can, such as in this case.

SoLoGHoST
01-10-2003, 09:57 AM
Thanks for the info Squirm, I usually just find a way to do it, without worrying so much on the consequences. But this is good to know!

Thanks!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum