0

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 :

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..

a1
b2
c3
d4
e5

and I would like to show label1 in for each second like..

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

like this...

Steve
  • 213,761
  • 22
  • 232
  • 286
Olgu Kivanc
  • 11
  • 1
  • 1
  • 6
  • **I can only see the last line in label** because you keep replacing the label text as the loop progresses: `Label1.Text = RichTextBox1.Lines(i)`. This is easy to see using the debugger. If you want to move all the text, append it – Ňɏssa Pøngjǣrdenlarp Feb 06 '16 at 20:40

4 Answers4

2

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.

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:

 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....

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

' 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
Steve
  • 213,761
  • 22
  • 232
  • 286
1

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 &= ...

Also note that you need to include something like Environment.NewLine in order to include line breaks.

For i As Integer = 0 To RichTextBox1.Lines.Length - 1
    Label1.Text &= RichTextBox1.Lines(i) & If(i < RichTextBox1.Lines.Length - 1, Environment.NewLine, "")
Next
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
0

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
Olgu Kivanc
  • 11
  • 1
  • 1
  • 6
  • @Plutonix it is evil to an extent. Its used to make sure that events get handled periodicaly when you're performing some long-running operation on the UI thread and his wouldn't need to be using it. I would recommend using a seperate thread as using `DoEvents` has the possibility of re-entrance... – Trevor Feb 07 '16 at 00:00
0

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
Ashish Tiwari
  • 2,168
  • 4
  • 30
  • 54