从 Richtextbox 中逐行读取并显示在标签中 (vb.net)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35246142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
read line by line from richtextbox and show in label (vb.net)
提问by Olgu Kivanc
I would like to read line by line from richtextbox and show each line every a second in label.
I have this code blocks.
and I think I need a timer but I couldnt make it.
can you help me ?
Remarks :
我想从 Richtextbox 中逐行读取,并在标签中每隔一秒显示每一行。
我有这个代码块。
我想我需要一个计时器,但我做不到。
你能帮助我吗 ?
评论 :
If I use this code , I can only see the last line in label.
如果我使用此代码,我只能看到标签中的最后一行。
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
I mean, assume that we have lines in richtextbox like..
我的意思是,假设我们在 Richtextbox 中有行,例如..
a1
b2
c3
d4
e5
and I would like to show label1 in for each second like..
我想每秒显示 label1 ,例如..
a1
(after 1 sec.)
b2
(after 1 sec.)
c3
(after 1 sec.)
like this...
像这样...
回答by Steve
You seems to expect that, because you set the Text property, the label repaints itself immediately with the new text. This doesn't happen until you exit from the event handler and the system could repaint the label. Of course, with this code, only the last text is shown.
您似乎期望,因为您设置了 Text 属性,标签会立即使用新文本重新绘制自身。在您退出事件处理程序并且系统可以重新绘制标签之前,这不会发生。当然,使用此代码,只显示最后一个文本。
To reach your goal, you could use a Timer set to 1 second interval and a counter that keeps track of the current line dispayed:
为了达到您的目标,您可以使用设置为 1 秒间隔的计时器和一个跟踪当前显示行的计数器:
Dim tm As System.Windows.Forms.Timer = new System.Windows.Forms.Timer()
Dim counter As Integer = 0
At this point your button click just start the timer and exits
此时您的按钮单击只是启动计时器并退出
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
When the Tick event is raised you change the label text to the line indexed by the counter, increment it and check if you have reached the last line restarting from the first one if this is the case. Note that the button is disabled before exiting. This is required to avoid a second/third/fourth/etc click on the same button while the timer runs..... More on Button2 later....
当引发 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
Of course, now you need another button to stop the Timer run and reenable the first button
当然,现在您需要另一个按钮来停止 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
回答by Visual Vincent
You're almost there. Your problem is that you keep setting the text, not adding to it. Label1.Text = ...sets the text, if you want to keep adding to it you'd use Label1.Text &= ...
您快到了。您的问题是您一直在设置文本,而不是添加文本。Label1.Text = ...设置文本,如果你想继续添加,你会使用Label1.Text &= ...
Also note that you need to include something like Environment.NewLinein order to include line breaks.
另请注意,您需要包含类似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
回答by Olgu Kivanc
thank you for your help !!! I solved with this code ;
感谢您的帮助 !!!我用这段代码解决了;
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
回答by antony thomas
It is very simple.
这很简单。
Declare one more string variable and load all string to this variable . Improved code is given below.
再声明一个字符串变量并将所有字符串加载到该变量中。改进后的代码如下。
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

