?
Solved

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

Posted on 2006-05-08
9
Medium Priority
?
178 Views
Last Modified: 2010-05-01
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
0
Comment
Question by:TDhanraj
9 Comments
 
LVL 13

Expert Comment

by:iHadi
ID: 16635656
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
 
LVL 19

Expert Comment

by:Shauli
ID: 16635896
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
 

Author Comment

by:TDhanraj
ID: 16635981
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:fullcontact
ID: 16637274
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
 
LVL 13

Expert Comment

by:iHadi
ID: 16637389
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
 

Author Comment

by:TDhanraj
ID: 16649415
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
 
LVL 13

Accepted Solution

by:
iHadi earned 2000 total points
ID: 16651536
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
 

Author Comment

by:TDhanraj
ID: 16661909
Is there a statement i can add to disreguard any lines that are blank within the file?
0
 

Author Comment

by:TDhanraj
ID: 16662790
never mind... i did a len of line test to skip blanks.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question