|
-
Jun 12th, 2004, 03:31 PM
#1
Thread Starter
Lively Member
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
-
Jun 12th, 2004, 06:14 PM
#2
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:
Private strLines() As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Lets Load the lines from the file and store them in a globally
Dim sr As New IO.StreamReader("..\File.txt")
strLines = Split(sr.ReadToEnd, vbCrLf)
sr.Close()
sr = Nothing
'Done Reading File ;)
End Sub
Private Sub MyTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyTimer.Tick
Static intCurrentIndex As Integer
lblLine.Text = strLines(intCurrentIndex)
If intCurrentIndex = strLines.Length - 1 Then
'Reset
intCurrentIndex = 0
Else
intCurrentIndex += 1
End If
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
MyTimer.Start()
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
MyTimer.Stop()
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|