Read text File, display first line scrolling in label bar then read next line

I am trying to create a scrolling label bar that can read in data from a file, then display the text scrolling across the form.  It should then proceed to next line and scroll and so on...  The problem I have is that when the file is read in, it doesn't wait for the text to scroll across the form before reading the next line so I continuously get the same line scrolling across or ever 3rd line scrolling across.  I am not opposed to doing this a different way say setting up a secondary folder for temporary files or making each line of the text file an individual file and reading the file to be displayed because i would also like to delete the files or line of text once it has become 2 hours old.

‘***here is my code so far*****


Private Sub Timer1_Timer()

        sFilePathIn = CurDir & "\SysMessages\SysAdminMsg"  ' Opens File and selects file name
        If Not fsoIn.FileExists(sFilePathIn) Then
            fsoIn.CreateTextFile (sFilePathIn)
        End If
        Set fFileIn = fsoIn.GetFile(sFilePathIn)
        Set tStreamIn = fFileIn.OpenAsTextStream(ForReading, TristateFalse)

        Do While tStreamIn.AtEndOfStream <> True  ' Loop through each line
        sBuffer = tStreamIn.ReadLine
       
 
    'When the label goes off the left side of the form,
    ' reposition it onto the right side.
 If (lblMarquee.Left + lblMarquee.Width) <= 0 Then
         lblMarquee.Left = Form1.Width
       
        lblMarquee = sBuffer
     
 End If
       
       lblMarquee.Left = lblMarquee.Left - 100

 Loop

        tStreamIn.Close
        Set tStreamIn = Nothing
        Set fFileIn = Nothing
        Set fsoIn = Nothing
        Close
        Close

End Sub
TDhanrajAsked:
Who is Participating?
 
iHadiConnect With a Mentor Commented:
Sorry for the delay. I've been a little busy lately.

Replace the following code with your code:

Dim fsoIn As New FileSystemObject
Dim fFileIn As File
Dim tStreamIn As TextStream
Dim FileOpen As Boolean ' Falg determines if the file is open

Private Sub Form_Load()
    Dim retVal As Long
   
    sldrSpeed.Max = 300
    sldrSpeed.Min = 1
    sldrSpeed.Value = 100
   
    Timer1.Enabled = False
    Timer1.Interval = sldrSpeed.Value
    Timer1.Enabled = False
   
     chkAnimate.Caption = "Start"
    'chkAnimate.Value = 1
    'chkAnimate.Visible = False
    'sldrSpeed.Visible = False
    Label2.Caption = ""
    Label3.Caption = ""
   
    'retVal = SetWindowText(Me.hWnd, Label1)
End Sub

'Check val to start scroll..
Private Sub chkAnimate_Click()
    If chkAnimate.Value = 0 Then
        Timer1.Enabled = False
        sldrSpeed.Enabled = False
        chkAnimate.Caption = "Start"
    Else
        chkAnimate.Caption = "Pause"
        Timer1.Enabled = True
        sldrSpeed.Enabled = True
    End If
End Sub


' Reset the timer to the value indicated by the slider control
Private Sub sldrSpeed_Scroll()
    Timer1.Interval = sldrSpeed.Value
End Sub

Private Sub Timer1_Timer()

If FileOpen = True Then
    If tStreamIn.AtEndOfStream <> True Then  ' Loop through each line
        'When the label goes off the left side of the form,
        ' reposition it onto the right side.
        If (lblMarquee.Left + lblMarquee.Width) <= 0 Then
            sBuffer = tStreamIn.ReadLine
            lblMarquee.Left = Form1.Width
            lblMarquee = sBuffer
        Else
            lblMarquee.Left = lblMarquee.Left - 100
        End If
    Else
        CloseFile
    End If
Else
    OpenFile
End If
End Sub

Private Sub OpenFile()
Dim sFilePathIn As String

sFilePathIn = CurDir & "\SysMessages\SysAdminMsg"  ' Opens File and selects file name
If Not fsoIn.FileExists(sFilePathIn) Then
    fsoIn.CreateTextFile (sFilePathIn)
End If
Set fFileIn = fsoIn.GetFile(sFilePathIn)
Set tStreamIn = fFileIn.OpenAsTextStream(ForReading, TristateFalse)
FileOpen = True
End Sub

Private Sub CloseFile()
    tStreamIn.Close
    Set tStreamIn = Nothing
    Set fFileIn = Nothing
    Set fsoIn = Nothing
    Close
    FileOpen = False
End Sub
0
 
iHadiCommented:
Private Sub Timer1_Timer()

        sFilePathIn = CurDir & "\SysMessages\SysAdminMsg"  ' Opens File and selects file name
        If Not fsoIn.FileExists(sFilePathIn) Then
            fsoIn.CreateTextFile (sFilePathIn)
        End If
        Set fFileIn = fsoIn.GetFile(sFilePathIn)
        Set tStreamIn = fFileIn.OpenAsTextStream(ForReading, TristateFalse)

    Do While tStreamIn.AtEndOfStream <> True  ' Loop through each line

        'When the label goes off the left side of the form,
        ' reposition it onto the right side.
        If (lblMarquee.Left + lblMarquee.Width) <= 0 Then
            sBuffer = tStreamIn.ReadLine
            lblMarquee.Left = Form1.Width
            lblMarquee = sBuffer
        Else
            lblMarquee.Left = lblMarquee.Left - 100
        End If
    Loop

    tStreamIn.Close
    Set tStreamIn = Nothing
    Set fFileIn = Nothing
    Set fsoIn = Nothing
    Close
    Close

End Sub

0
 
ShauliCommented:
Locate 2 command buttons on your form, command1 and command2, locate a lable (lable1) on your form.
Command1 read the file, and commad2 move to the next line. This is just an example and you can modify any way you want:


Option Explicit
Private cmdClick As Boolean

Private Sub Command1_Click()
Dim ff As Integer, tmpFileString As String, mySplit() As String, dLoop As Integer
'read file
ff = FreeFile
Open "C:\test2.txt" For Binary As #ff
    tmpFileString = Space$(LOF(ff) - 1)
    Get #ff, , tmpFileString
Close #ff
'split file to lines
mySplit = Split(tmpFileString, vbNewLine, -1)
'display one line at a time
For dLoop = 0 To UBound(mySplit)
    Label1.Caption = Label1.Caption & mySplit(dLoop) & vbNewLine
    cmdClick = False
        Do While cmdClick = False
            DoEvents
        Loop
Next dLoop
End Sub

Private Sub Command2_Click()
cmdClick = True
End Sub

S
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
TDhanrajAuthor Commented:
iHadi,

I tried that and it just scrolls way too fast... How do i slow it down?  

Private Sub Form_Load()
    Dim retVal As Long
   
    sldrSpeed.Max = 100
    sldrSpeed.Min = 1
    sldrSpeed.Value = 100
   
    Timer1.Enabled = False
    Timer1.Interval = sldrSpeed.Value
    Timer1.Enabled = False
   
     chkAnimate.Caption = "Pause"
    'chkAnimate.Value = 1
    'chkAnimate.Visible = False
    'sldrSpeed.Visible = False
    Label2.Caption = ""
    Label3.Caption = ""
   
    retVal = SetWindowText(Me.hwnd, Label1)
End Sub

'Check val to start scroll..
Private Sub chkAnimate_Click()
    If chkAnimate.Value = 0 Then
        Timer1.Enabled = False
        sldrSpeed.Enabled = False
        chkAnimate.Caption = "Start"
    Else
        chkAnimate.Caption = "Pause"
        Timer1.Enabled = True
        sldrSpeed.Enabled = True
    End If
End Sub


' Reset the timer to the value indicated by the slider control
Private Sub sldrSpeed_Scroll()
    Timer1.Interval = sldrSpeed.Value
End Sub

Private Sub Timer1_Timer()

        sFilePathIn = CurDir & "\SysMessages\SysAdminMsg"  ' Opens File and selects file name
        If Not fsoIn.FileExists(sFilePathIn) Then
            fsoIn.CreateTextFile (sFilePathIn)
        End If
        Set fFileIn = fsoIn.GetFile(sFilePathIn)
        Set tStreamIn = fFileIn.OpenAsTextStream(ForReading, TristateFalse)

        Do While tStreamIn.AtEndOfStream <> True  ' Loop through each line
        sBuffer = tStreamIn.ReadLine
       
 
    'When the label goes off the left side of the form,
    ' reposition it onto the right side.
 If (lblMarquee.Left + lblMarquee.Width) <= 0 Then
         lblMarquee.Left = Form1.Width
       
        lblMarquee = sBuffer
     
 End If
       
       lblMarquee.Left = lblMarquee.Left - 100

 Loop

        tStreamIn.Close
        Set tStreamIn = Nothing
        Set fFileIn = Nothing
        Set fsoIn = Nothing
        Close
        Close

End Sub
0
 
fullcontactCommented:
You will find that Timer1.Interval is in milliseconds, so from 1 to 100 as a range will be 1 to 100 milliseconds as a delay.
Try mulitplying this by a factor until you are happy with the speed.

i.e Timer1.Interval = sldrSpeed.Value * 10 (this will give 10ms to 1 sec as the delay.)

0
 
iHadiCommented:
You are scrolling the label using the do...loop that varies in speed depending on your computers cpu. This way has bad impact on the cpu usage. To justify that you need to scroll the label in a timer that has a static interval on all systems and you can control the speed of the scrolling without using much cpu overhead. I'll post some code within a few hours.
0
 
TDhanrajAuthor Commented:
iHadi,

Did you ever get around to putting together that code?  Would it be easier if i zipped up the app and sent it to you?
0
 
TDhanrajAuthor Commented:
Is there a statement i can add to disreguard any lines that are blank within the file?
0
 
TDhanrajAuthor Commented:
never mind... i did a len of line test to skip blanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.