您似乎期望,因为您设置了 Text 属性,所以标签会立即使用新文本重新绘制自身。在您退出事件处理程序并且系统可以重新绘制标签之前,这不会发生。当然,使用此代码,只显示最后的文本。
为了达到您的目标,您可以使用设置为 1 秒间隔的 Timer 和一个跟踪当前显示行的计数器:
Dim tm As System.Windows.Forms.Timer = new System.Windows.Forms.Timer()
Dim counter As Integer = 0
此时您的按钮单击只需启动计时器并退出
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
tm.Interval = 1000
AddHandler tm.Tick, AddressOf onTick
tm.Start()
' Don't allow to click again this button until
' the timer is stopped
Button1.Enabled = False
Button2.Enabled = True
End Sub
引发 Tick 事件时,您将标签文本更改为由计数器索引的行,将其递增并检查是否已到达从第一行重新开始的最后一行(如果是这种情况)。请注意,该按钮在退出前被禁用。这是为了避免在计时器运行时第二次/第三次/第四次/等点击同一个按钮.....稍后将详细介绍 Button2....
Sub onTick(sender as Object, e as EventArgs)
Label1.Text = RichTextBox1.Lines(counter)
counter += 1
if counter >= RichTextBox1.Lines.Count Then
counter = 0
End If
End Sub
当然,现在您需要另一个按钮来停止 Timer 运行并重新启用第一个按钮
' This button stops the timer and reenable the first button disabling
' itself - It should start as disabled from the form-designer
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
tm.Stop
RemoveHandler tm.Tick, AddressOf onTick
Button1.Enabled = True
Button2.Enabled = False
End Sub