Is there any Visual Basic 2008 scrolling text code

2019-08-20 10:44发布

问题:

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!

回答1:

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.



回答2:

Its more easier with a timer:

Lable.Text = Lable1.Text.Substring(1) & Lable1.Text.Substring(0, 1)

Put this code timer and start timer on form load that's how you can use it