Home Ask Login Register

Developers Planet

Your answer is one click away!

Olgu Kivanc February 2016

read line by line from richtextbox and show in label (vb.net)

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

Answers


Visual Vincent February 2016

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


Steve February 2016

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 


Olgu Kivanc February 2016

I get errors

BC30676 'Tick' is not an event of 'Object'.
BC30577 'AddressOf' operand must be the name of a method

I am using VB 2015, codes are here:

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


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        tm.Interval = 1000
        AddHandler tm.Tick, AddressOf (onTick)
        tm.Start()
    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
End Class


Olgu Kivanc February 2016

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

Post Status

Asked in February 2016
Viewed 3,588 times
Voted 11
Answered 4 times

Search




Leave an answer


Quote of the day: live life