目前我正在使用这个我在网上找到的vb.net代码,下面是一个表格的scoll文本。有没有办法让muliple文本同时滚动?所以对于我的数组,我在Item1下方有Item2,依此类推,具体取决于数组中有多少项?我希望每个新项目都在下一个项目的下方滚动。
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
答案 0 :(得分: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
答案 1 :(得分: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 =?)我不确定这段代码是否准确,但我很确定你能看到这个概念。