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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a text file line by line by clicking the Next Button 1

Status
Not open for further replies.

SysAdmMke

Technical User
Jan 18, 2006
34
US
Private Sub cmdLoadConfig_Click()
Dim strTextLine As String

Open "C:\abcd.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, strTextLine
txtRangeSelect.Text = strTextLine
Loop
Close #1
End Sub

I can understand on how to read in file but how do i loop for when the user clicks the next button. I just need to read one line each time the Next Button is pressed.

 


hi,

One procedure OPENS the file.

The button click event does the LINE INPUT statement

When done, in whatever event indicates that, you CLOSE the file

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Thanks Skip

So when I click the button,it will read the line one line at a time each time i hit the button? This is what I am trying to do. Intead of reading thr entire file in at once.

-Mike
 


Well did you try it. Does not cost a dime!

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
well how to do call the button event from the load form event?
it seems it won't work unless the variables are declared in that procedure.

 



You must declare your variables at the module level. See HELP on Scope.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Just because you want to show the users one line at a time, does not mean you have to read it in one line at a time.
Create a form member that is a list/collection/array of all the lines.
When the user hits the next button then you just increment the line number to pull from.

Dim myFileLines as new List()
Dim currentRow as Integer = 1;

form_load()
'Read whole file into list

button click()
currentRow += 1
lblFileLine = myFileLines(currentRow)


Lodlaiden


You've got questions and source code. We want both!
 

First thing, you are using the old VB 6 method of reading a text file. I suggest using the .NET System.IO file functions. First, declare a StreamReader object at form level (i.e., right after the "Public Class <FormName> line).

Dim sr As System.IO.StreamReader

In the Form_Load event, load the file into the streamreader:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

sr = New System.IO.StreamReader("C:\abcd.txt")

End Sub

Then in the button click event:

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

Dim s As String

s = sr.ReadLine()

txtRangeSelect.Text = s

End Sub

Somewhere you need to close the file when finished, so maybe in the FormClosing event put:

sr.Close()

That should do it.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
jebenson

Thanks guy! This works great and just want I wanted. Appreciate your help and all the rest of guys the help! Yes I could have read it into an array as well. If I want to go previous text, then I guess I would have to do that.

Like have a back button. The only read to go back would have it in a array correct?

Thanks again
Mike
 
Yes. Most of the file reading classes are forward read only.
That is why I suggested the collection/array.

If you are not editing the file, then you don't really have a need to keep an open connection to it. Granted you can set options for read only, but it's still hanging there with an open connection.


BTW, Trust not the user. They will walk away leaving the file locked and open. Depending on how the system disconnects from the file they may have to call you be able to open it again.

Lodlaiden

You've got questions and source code. We want both!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top