0

我想从richtextbox逐行读取并在标签中每秒显示每一行。
我有这个代码块。
我想我需要一个计时器,但我做不到。
你能帮助我吗 ?
评论 :

如果我使用此代码,我只能看到标签中的最后一行。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim RotateCount As String()
    For i As Integer = 0 To RichTextBox1.Lines.Length - 1
        Label1.Text = RichTextBox1.Lines(i)
    Next
End Sub

我的意思是,假设我们在richtextbox 中有行,例如..

a1
b2
c3
d4
e5

我想每秒钟都显示 label1 像..

a1 
(after 1 sec.)
b2 
(after 1 sec.)
c3 
(after 1 sec.)

像这样...

4

4 回答 4

2

您似乎期望,因为您设置了 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
于 2016-02-06T20:55:49.357 回答
1

您快到了。您的问题是您一直在设置文本,而不是添加文本。Label1.Text = ...设置文本,如果你想继续添加你会使用Label1.Text &= ...

另请注意,您需要包含类似Environment.NewLine的内容才能包含换行符。

For i As Integer = 0 To RichTextBox1.Lines.Length - 1
    Label1.Text &= RichTextBox1.Lines(i) & If(i < RichTextBox1.Lines.Length - 1, Environment.NewLine, "")
Next
于 2016-02-06T20:40:14.167 回答
0

谢谢您的帮助 !!!我用这段代码解决了;

    Public Class Form1
    Dim tm = New System.Windows.Forms.Timer()
    Dim counter As Integer = 0


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    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

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        For i = 0 To RichTextBox2.Lines.Count - 1
            TextBox1.Text = RichTextBox2.Lines(i)
            wait(2000)
        Next
    End Sub

    Private Sub wait(ByVal interval As Integer)
        Dim sw As New Stopwatch
        sw.Start()
        Do While sw.ElapsedMilliseconds < interval
            ' Allows UI to remain responsive
            Application.DoEvents()
        Loop
        sw.Stop()
    End Sub
End Class
于 2016-02-06T21:48:46.790 回答
0

这很简单。

再声明一个字符串变量并将所有字符串加载到该变量中。改进的代码如下。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As system.EventArgs) Handles Button1.Click
   Dim a1 as int32
   a1=0
  'get number of lines of the rich text box content
   a1=RichTextBox1.Lines.Count()
   Dim str As String
    For i As Int32 = 0 To a1-1
        str = str + RichTextBox1.Lines(i)
    Label1.Text= str
    Next
End Sub
于 2016-07-23T08:08:16.140 回答