Results 1 to 2 of 2

Thread: reading from a text file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    91

    reading from a text file

    I need to read a text file on a timer. on load i wantt o open the file and then the timer tick i want it to get a line. every tick i want it to go to another line. i have no idea how to do this nor do i have space on my computer for the mdsn library. tks

  2. #2
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    Heres a small sample:

    Add a Text File to your project called "File.txt"

    Put the following in the text file:

    Code:
    Line 1
    Line 2
    Line 3
    Line 4
    Line 5
    Line 6
    ---

    Add the following to a form

    Button (Name "btnStart", Text "Start")
    Button (Name "btnStop", Text "Stop")
    Label (Name "lblLine", Text "")
    Timer (Name "MyTimer", Interval "500")

    ---

    Add this code to the form



    VB Code:
    1. Private strLines() As String
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.  
    5.         'Lets Load the lines from the file and store them in a globally
    6.         Dim sr As New IO.StreamReader("..\File.txt")
    7.  
    8.         strLines = Split(sr.ReadToEnd, vbCrLf)
    9.  
    10.         sr.Close()
    11.  
    12.         sr = Nothing
    13.  
    14.         'Done Reading File ;)
    15.  
    16.     End Sub
    17.  
    18.     Private Sub MyTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyTimer.Tick
    19.         Static intCurrentIndex As Integer
    20.  
    21.         lblLine.Text = strLines(intCurrentIndex)
    22.  
    23.         If intCurrentIndex = strLines.Length - 1 Then
    24.             'Reset
    25.             intCurrentIndex = 0
    26.         Else
    27.             intCurrentIndex += 1
    28.         End If
    29.     End Sub
    30.  
    31.     Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    32.         MyTimer.Start()
    33.     End Sub
    34.  
    35.     Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
    36.         MyTimer.Stop()
    37.     End Sub
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width