read line by line from richtextbox and show in label (vb.net) - 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...

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

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

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

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

Related

Refreshing text in a TextBox at intervals of 1 second

I am attempting to display the local time in a text box but have it refresh... I used a timer to hopefully refresh the time, but it does not seem to reprint my text. If you could take the time to help me out that would be great!
EDIT*** So I attempted this with TextBox.AppendText() to see what happens if it continually reprints and I noticed that the date and time does not update at all. Do I need to refresh the form???
Public Class Form1
Dim t As String = My.Computer.Clock.LocalTime
Dim m As String = t & vbCrLf & " - Time Left - "
Private Timer As System.Windows.Forms.Timer
Private TimerCounter As Integer = 0
Dim TempText As String = m
Protected Sub TimerTick(ByVal sender As Object, ByVal e As EventArgs)
TextBox.TextAlign = HorizontalAlignment.Center
TimerCounter += 1
TextBox.Text = t
End Sub
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Shown 'this goes with the line just above
Timer = New Windows.Forms.Timer With {.Interval = 1000}
AddHandler Timer.Tick, AddressOf TimerTick
Timer.Start()
End Sub
End Class
My expected result if for the local time to update in the textbox1 each time the timer ticks.
You set the variable t at the moment of its declaration, but then you never update it. So it contains always the same value.
In reality you don't even need that variable. You can set simply the TextBox.Text to the My.Computer.Clock.LocalTime
Protected Sub TimerTick(ByVal sender As Object, ByVal e As EventArgs)
' You can set this property just one time when you define your TextBox
' TextBox.TextAlign = HorizontalAlignment.Center
TimerCounter += 1
TextBox.Text = My.Computer.Clock.LocalTime
End Sub

Windows form: Disable autoscrolling on DataGridView

I have a DataGridView that I continuously fill adding rows with a BackgroundWorker.
The problem is that even though each row is added on the bottom of the DataGridView the scrollbar continues to move upwards instead of remaining
on the same spot in which I moved it before.
How can I disable the autoscrolling to keep the DataGridView on the same spot while new rows are added at the bottom?
I usually do this with a ListView and I am sure a DataGridView would work similarly:
If ListView1.Items.Count > 0 Then
ListView1.Items.Item(ListView1.Items.Count -1).EnsureVisible()
End If
This will ensure that your last entry is visible.
You can use DataGridView1_Scroll event for fetching the specific scroll position. like below -
Dim savedScrollPosition As Integer = 0
Private Sub DataGridView1_Scroll(sender As Object, e As System.Windows.Forms.ScrollEventArgs) Handles DataGridView1.Scroll
savedScrollPosition = e.NewValue
End Sub
Then you can assign this to DataGridView property FirstDisplayedScrollingRowIndex after adding new row. like below -
Public Class Form1
Dim i As Integer = 1
Dim savedScrollPosition As Integer = 0
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim timer As New Timer()
timer.Interval = 1000
AddHandler timer.Tick, AddressOf timer_Tick
timer.Start()
End Sub
Private Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
UpdateDGV()
i = i + 1
End Sub
Private Sub UpdateDGV()
DataGridView1.ColumnCount = 2
DataGridView1.Columns(0).Name = "Product_Name"
DataGridView1.Columns(1).Name = "Product_Price"
Dim row As String() = New String() {"Product # " + Convert.ToString(i), 1000 + i}
DataGridView1.Rows.Add(row)
'Keep your scrollbar at specific position
DataGridView1.FirstDisplayedScrollingRowIndex = savedScrollPosition
'keep your scrollbar at bottom of the grid
'DataGridView1.FirstDisplayedScrollingRowIndex = DataGridView1.RowCount - 1
End Sub
Private Sub DataGridView1_Scroll(sender As Object, e As System.Windows.Forms.ScrollEventArgs) Handles DataGridView1.Scroll
savedScrollPosition = e.NewValue
End Sub
End Class

I want to display a list of names(3) from a textbox(user input) into a label

This is the code I have so far but is not working properly. The textbox for the names user should input and the button show to display the names in a label in the order entered.
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim intcount, w1, w2, w3 As Integer
Dim intMax As Integer = 2
For intcount = 0 To intMax
strSurnames(intcount) = TextBox1.Text
Next
TextBox1.Clear()
End Sub
Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
Dim intMax As Integer = 2
For intcount = 0 To intMax
lblShow.Text &= strSurnames(intcount)
Next
End Sub
I am guessing you are going to use only 1 textbox and click the "add button" multiple times to store the name. If this is true, you will first need to create
Dim arrayStr As New List(Of String)
Every single time you click on the add button, it will add into this array.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
arrayStr.Add(TextBox1.Text.Trim())
TextBox1.Clear()
End Sub
And to show the full name in just one label when you click on the "Show button", can do it like this
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label1.Text = "" //The label text is cleared.
For i As Integer = 0 To arrayStr.Count() - 1
Label1.Text += arrayStr(i) + ", "
Next
End Sub
UPDATE - BASED ON YOUR COMMENT QUESTION
This is the updated solution based on your question. I am only going to show you with the "Sur Names" only. You can implement the "Weight" the same way.
First, create your array and declare a count as integer for the size of your array.
Dim surNameStr(20) As String
Dim count As Integer = 0
In the "Add Button", you increase the count number by 1 every time you add a new sur name. Once it reached your "maximum" number, you disable your button by BtnAdd.Enabled = False.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If count < 20 Then
surNameStr(count) = TextBox1.Text.Trim()
count = count + 1
Else
Button1.Enabled = False
Button2.Enabled = True
End If
End Sub
Then, in "Show Button", this is how you can show all the surnames stored.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label1.Text = ""
For i As Integer = 0 To 19
Label1.Text += surNameStr(i) + vbNewLine
Next
End Sub

VB.NET: Label doesn't refresh

I think that these line of code should work properly, but the label never updates it's value... Any idea what am I missing?
Timer is Enabled from properties...
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim out As Integer = 13
Dim s As String
s = Format(out, "0#")
Label1.Text = s
Label1.Refresh()
out -= 1
End Sub

Rich Text Box VB.NET Line By Line Stop and auto?

I have this code:
If RotateCount = RichTextBox1.Lines.Count Then
RotateCount = 0
End If
TextBox2.Text = RichTextBox1.Lines(RotateCount)
RotateCount += 1
The Question is... How do i get it to STOP after it reads the last line and not keep repeating?
Also, is there a way to automate it so I dont have to keep hitting the button to go to next one?
(for those who need to know this is for an automated Twitter poster for a Cryptocurrency Tipping Bot)
It wont stop because you re-assign RotateCount = 0 when RotateCount = RichTextBox1.Lines.Count it can be avoided by using
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If RotateCount = RichTextBox1.Lines.Count Then
Exit Sub
End If
TextBox1.Text = RichTextBox1.Lines(RotateCount)
RotateCount += 1
End Sub
If you want to do it automatically means you need to call a time delay between read using a timer. you can achieve this by placing a timer control from the tool box and then set its interval to 5000(5sec) and enable it on button click. then the code will be
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If RotateCount = RichTextBox1.Lines.Count Then
Exit Sub
End If
TextBox1.Text = RichTextBox1.Lines(RotateCount)
RotateCount += 1
End Sub