3 Replies - 638 Views - Last Post: 26 November 2015 - 01:46 AM Rate Topic: -----

#1 digituser   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 09-August 15

How to read the line from the text file where i stop in perviously ?

Posted 24 November 2015 - 09:33 AM

I'm trying to read the line continuously from the text file where i Stop in before.Actually the Form1 have one radspinEditor and one button. In the radspinEditor i give some value (EX: radspineditor1 value is 10 ).Then i press the button it will read only ten lines from the mytextfile.txt file and display in label
Next time i will give 10 in the radspinEditor ,it start Displaying from 11th line not Form the 1st.
if i reach the last line means again append from the first line to display . (it like a circular buffer )

So give any idea how to do ?
 mytextfile.txt

 read
 walk
 book
...

upto 500 lines is there 




Is This A Good Question/Topic? 0
  • +

Replies To: How to read the line from the text file where i stop in perviously ?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15427
  • View blog
  • Posts: 61,808
  • Joined: 12-June 08

Re: How to read the line from the text file where i stop in perviously ?

Posted 24 November 2015 - 09:52 AM

Why would you want to do that?

At best you would read all the lines in and just display what you need. At worst you would read the lines in for 10.. and when you want 11 do the read lines again until you come to the 11th and then add that to the text box.
Was This Post Helpful? 0
  • +
  • -

#3 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1538
  • View blog
  • Posts: 3,869
  • Joined: 01-February 13

Re: How to read the line from the text file where i stop in perviously ?

Posted 25 November 2015 - 04:27 AM

You could also use a class scoped StreamReader to do this. When your Form is loading you can create the new streamreader. Then each time your button is pressed you use a For Next loop to iterate from 1 to the number that the user selected in the NumericUpDown control.

Inside the loop you can first check if the end of the stream was reached, if it was you can reset the position of the stream back to the beginning.

After that, read a line from the stream (file) and append it to the Label`s Text using the &= operator.

However, do not forget to close the StreamReader when your form is closing or when you no longer need to read any more lines from the file.

Here is a simple commented example you can check out.
Public Class Form1
    Private sr As IO.StreamReader = Nothing

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        '(IMPORTANT) when the form is closing don`t forget to close the stream
        If sr IsNot Nothing Then sr.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'create the new StreamReader
        sr = New IO.StreamReader("C:\TestFolder\MyFile.txt")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = "" 'reset the Label`s text

        'a loop that will iterate from 1 to the number the user selects in the NumericUpDown control
        For i As Integer = 1 To CInt(NumericUpDown1.Value)

            'if the end of the stream (file) is reached then reset its position back to the beginning
            If sr.EndOfStream Then sr.BaseStream.Seek(0, IO.SeekOrigin.Begin)

            'read a line from the file and append it to the Label`s text
            Label1.Text &= sr.ReadLine & vbNewLine

        Next
    End Sub
End Class


Was This Post Helpful? 1
  • +
  • -

#4 digituser   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 09-August 15

Re: How to read the line from the text file where i stop in perviously ?

Posted 26 November 2015 - 01:46 AM

Thank u, @ Iron Razer. i did in different way .Anyway lot of thanks for ur reply... :clap: :clap: :clap: :bananaman:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1