0

目前我正在使用我在网上找到的这个 vb.net 代码,在下面将文本添加到表单中。有没有办法同时滚动多个文本?因此,对于我的数组,我将 Item2 放在 Item1 的正下方,依此类推,具体取决于数组中有多少项?我希望每个新项目都滚动到下一个项目的正下方。

 Option Strict Off  'Strict has to be off for the font
Public Class Form1

   Dim sItems() As String = {"Item1", "Item2", "Item3", "Item4"}

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      For index As Integer = 0 To sItems.Length - 1
         animateText(sItems(index), Button1.Location.Y - 50, Button1.Location.Y - 50, Button1.Location.X)
      Next
   End Sub
   Dim label As New Label
   Dim maxHeight As Integer
   Dim colour As Integer = 255
   Public Sub animateText(ByVal text As String, ByVal lowerBounds As Integer, ByVal upperBounds As Integer, ByVal xPos As Integer)
         maxHeight = upperBounds
         label.Text = text
         label.TextAlign = ContentAlignment.MiddleCenter
         label.Location = New Point(xPos, lowerBounds)
         label.Font = New Font(Font.Bold, 20)
         label.AutoSize = True
         Me.Controls.Add(label)
      Timer1.Enabled = True
   End Sub
   Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
      label.Location = New Point(label.Location.X, label.Location.Y - 20)  'here's where you set the speed
      If label.Location.Y = maxHeight Then
         Timer1.Enabled = False
         Me.Controls.Remove(label)
      End If
   End Sub
End Class
4

2 回答 2

1

您需要创建一个标签列表,而不是只使用一个。

试试这个:

Public Class Form1

    Private maxHeight As Integer
    Private labels As New List(Of Label)
    Private sItems() As String = {"Item1", "Item2", "Item3", "Item4"}

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each lbl As Label In labels
            Me.Controls.Remove(lbl)
        Next
        labels.Clear()

        maxHeight = Button1.Bounds.Bottom
        For index As Integer = 0 To sItems.Length - 1
            animateText(sItems(index), Button1.Location.X)
        Next
        Timer1.Start()
    End Sub

    Public Sub animateText(ByVal text As String, ByVal xPos As Integer)
        Dim lbl As New Label
        lbl.Text = text
        lbl.TextAlign = ContentAlignment.MiddleCenter
        lbl.Font = New Font(Font.Bold, 20)
        lbl.AutoSize = True
        lbl.Location = New Point(xPos, If(labels.Count = 0, Button1.Bounds.Bottom, labels(labels.Count - 1).Bounds.Bottom))
        labels.Add(lbl)
        Me.Controls.Add(lbl)
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim lbl As Label
        For i As Integer = labels.Count - 1 To 0 Step -1
            lbl = labels(i)
            lbl.Location = New Point(lbl.Location.X, lbl.Location.Y - 20)
            If lbl.Bounds.Top <= maxHeight Then
                labels.RemoveAt(i)
                Me.Controls.Remove(lbl)
            End If
        Next
        If labels.Count = 0 Then
            Timer1.Stop()
        End If
    End Sub

End Class
于 2013-09-19T15:51:27.643 回答
0

首先,您应该从显示两行开始。当第一行滚动出屏幕时,在下面添加下一行并删除不再可见的行。

由于您将有 2 行,因此您的高度计算必须是当前值的 1/2。

在您的计时器循环中,您必须检测换行条件或结束条件,而不是结束循环。当前行应该是一个模块化的 var。从您的 button_click 事件中删除循环(而不是在计时器内循环)。

像这样更改计时器循环代码:

private index as integer '=0
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  label.Location = New Point(label.Location.X, label.Location.Y - 20)  'here's where you set the speed
  If label.Location.Y = (maxHeight / 2) Then
     If index < 3 Then 'next line
        Label.Text = sItems(index) & vbCrLf & sItems(index+1)
     Else 'last line
        Label.Text = sItems(index)
     End If 
     'reset the top
     label.Location = New Point(label.Location.X, label.Location.Y + (maxHeight/2))

     index += 1
  ElsIf label.Location.Y >= maxHeight Then
     Timer1.Enabled = False
     Me.Controls.Remove(label)
  End If
End Sub

没有看到你的整个表单定义(即 maxHeight = ?)我不确定这段代码是否准确,但我很确定你可以看到这个概念。

于 2013-09-19T13:58:41.707 回答