views:

406

answers:

1

I want a code that when applied to text in Visual Basic 2008 it scrolls across the screen. I don't want anything flashy, just something basic to start off with. If you guys know of such a thing that would be great!

EDIT:

I would like it to cycle, hope this makes it easier!

A: 

What you need: A single label, you can name it anything but in this case Label1 is our label, and the form file name is Form1.vb but of course you can change this.

What you still have to do: Edit the text scrollLabel(15) in both cases to make it the speed you want. The time is in milliseconds, between iterations.

There is probably a better way to do this, but here is my best shot:

Public Class Form1
    Dim IsClosed As Boolean = False
    Private Sub wait(ByVal time)
        Dim sw As New Stopwatch
        sw.Start()
        Do While sw.ElapsedMilliseconds < time
            Application.DoEvents() 'Lets our UI remain active
        Loop
    End Sub
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        IsClosed = True
    End Sub
    Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        scrollLabel(15)
    End Sub
    Private Sub scrollLabel(ByVal time)
        Dim passed As Boolean = False 'Indicates whether or not we have passed the initial bounds of the form
        Dim startX As Integer = Label1.Bounds.X
        For i As Integer = 0 To Me.Bounds.Width + Label1.Bounds.Width Step 1
            wait(time)
            Label1.SetBounds(Label1.Bounds.X - 1, Label1.Bounds.Y, Label1.Bounds.Width, Label1.Bounds.Height)
            If i > Me.Width - startX And passed = False Then
                Label1.SetBounds(Me.Width, Label1.Bounds.Y, Label1.Bounds.Width, Label1.Bounds.Height)
                passed = True
            End If
            If IsClosed = True Then
                Return
            End If
        Next
        scrollLabel(15)
    End Sub
End Class

Notice how IsClosed helps to break the loop to ensure the application does not continue once it is shut.

Also, if the user has resized the form while it is scrolling it may cause the label to jump when it hits the left side, but that will correct itself once it has completed a full loop.

Cyclone
The code looks promising but however it came up with a few errors, such as the Is Closed event. Still working on it though, I'll let you know If I make any progress.
Tony C
It worked for me, what errors did you get?
Cyclone
I think that active wait loop is just silly in a Form. Why not use a `Timer` instead?
Martinho Fernandes
I dont really like timers too much. I also didnt want to make this any more difficult than it had to be, and I didnt run it on a separate thread, he can do that if he wants to.
Cyclone