我想从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.)
像这样......
答案 0 :(得分:2)
您似乎期望这样,因为您设置了Text属性,标签会立即使用新文本重新绘制自身。直到您从事件处理程序退出并且系统可以重新绘制标签时才会发生这种情况。当然,使用此代码,只显示最后一个文本。
要达到目标,您可以使用设置为1秒间隔的计时器和一个跟踪当前行调度的计数器:
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
此时点击按钮即可启动计时器并退出
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
当提示Tick事件时,您将标签文本更改为计数器索引的行,将其递增并检查是否已到达从第一行重新开始的最后一行(如果是这种情况)。请注意,退出前该按钮已禁用。这是为了避免在计时器运行时对同一个按钮进行第二次/第三次/第四次/等等点击.....更多关于Button2的信息....
' 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
当然,现在您需要另一个按钮来停止计时器运行并重新启用第一个按钮
{{1}}
答案 1 :(得分: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
答案 2 :(得分: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
答案 3 :(得分: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