我想要一个代码,当应用于Visual Basic 2008中的文本时,它会在屏幕上滚动。我不想要任何华而不实的东西,只是一些基本的东西开始。如果你们知道这样的事情会很棒!

编辑:

我希望它循环,希望这会让它变得更容易!

有帮助吗?

解决方案

你需要什么:一个标签,你可以命名它,但在这种情况下Label1是我们的标签,表格文件名是Form1.vb但当然你可以改变这个。

您还需要做什么:在两种情况下编辑文本scrollLabel(15)以使其达到您想要的速度。迭代之间的时间以毫秒为单位。

可能有更好的方法可以做到这一点,但这是我最好的镜头:

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

注意IsClosed如何帮助打破循环以确保应用程序在关闭后不会继续。

此外,如果用户在滚动时调整了表单大小,可能会导致标签在向左侧跳转时跳转,但一旦完成完整循环,标签就会自动纠正。

其他提示

使用计时器更容易:

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

将此代码计时器和启动计时器放在表单加载上,即使用它的方式

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top