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
Related
I wrote a program which is checking for mails. It is comparing the mails with a list in Excel. If the mail was found in the list as well, it is moving them.
I did include a Progress-bar Control in the program, and it is working. But not as it should be. Once the program moved all the mails, the Progress-bar Control starts to move.
After a lot of research I found out, that it's not possible to interact with the GUI. I need to 'stop' my Code first. I tried that, But none of the solutions I found did work out for me.
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
'Do
'Percentage = Count / (Anzahl_mail + 1)
'Percentage = Percentage * 100
Dim numToDo As Integer = CInt(e.Argument)
For n As Integer = 1 To numToDo
System.Threading.Thread.Sleep(100)
BackgroundWorker1.ReportProgress(Convert.ToInt32((n / numToDo) * 100))
'BackgroundWorker1.ReportProgress(Percentage)
Next
'Loop While Not BackgroundWorker1.CancellationPending
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, _
e As System.ComponentModel.ProgressChangedEventArgs) _
Handles BackgroundWorker1.ProgressChanged
If ProgressBar1.Value = ProgressBar1.Maximum Then
ProgressBar1.Value = ProgressBar1.Minimum
End If
Me.ProgressBar1.Value = DirectCast(e.ProgressPercentage, Integer)
'Me.Invoke(New MethodInvoker(Sub() Me.ProgressBar1.Value = e.ProgressPercentage))
End Sub
I have an assignment for school where I have an animated character that runs. I need to be able make him jump and return to the spot he started the jump after a button press. I have a timer that switches images to animate the character. There are also buttons to speed up, slow down, start, and stop the character. I'm having trouble because I think I have to integrate the timer into the button press and I'm not too sure how to to that. There is a space at the bottom of the code where the jump button is. I'm able to make him go up but can't figure out how to make him come back down.
Here is my code so far:
Private Sub tmrSpeed_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrSpeed.Tick
Static Dim intCounter As Integer
picRunner.Location = New Point(picRunner.Location.X + 15, picRunner.Location.Y)
If picRunner.Location.X >= 523 Then
picRunner.Location = New Point(-25, picRunner.Location.Y)
End If
Select Case intCounter
Case 0
Me.picRunner.Image = My.Resources.r0
Case 1
Me.picRunner.Image = My.Resources.r1
Case 2
Me.picRunner.Image = My.Resources.r2
Case 3
Me.picRunner.Image = My.Resources.r3
Case 4
Me.picRunner.Image = My.Resources.r4
Case 5
Me.picRunner.Image = My.Resources.r5
Case 6
Me.picRunner.Image = My.Resources.r6
Case 7
Me.picRunner.Image = My.Resources.r7
Case 8
Me.picRunner.Image = My.Resources.r8
Case 9
Me.picRunner.Image = My.Resources.r9
End Select
intCounter += 1
If intCounter >= 10 Then
intCounter = 0
End If
End If
End Sub
Private Sub btnFaster_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFaster.Click
Try
Me.tmrSpeed.Interval = Me.tmrSpeed.Interval - 10
Catch x As Exception
MessageBox.Show("He can't run any faster!")
End Try
End Sub
Private Sub btnSlower_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSlower.Click
Me.tmrSpeed.Interval = Me.tmrSpeed.Interval + 10
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
Me.tmrSpeed.Stop()
End Sub
Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click
Me.tmrSpeed.Start()
End Sub
Private Sub btnJump_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnJump.Click
End Sub
End Class
How would I go about implementing the jump? My teacher says I have to use a loop, obviously to check when the picturebox reaches a certain hot but I can't figure out how to use this with the timer. Any help would be appreciated.
set a loop that calculates force.
also make a variable for the rate of decay for that force.
have the loop apply the force by subtracting it from the current y position
and after it apply force subtract the rate of decay from it to decrease the force so the next time the loop happens the force is lesser and it begins slowing down.
also have a global variable called gravity to create a constant pull on the object downwards so when the upward force decays it will go back down.
The rest you must figure out, if this is an assignment then it is very important for you to develop the logic to do what i described without someone giving you the answer. It will give you better understanding of the code.
I had the exact same assignment, and I came to this following code as my solution. I set the initial interval of the timer to 999999 so that the form wouldn't load with the animation jumping.
Private Sub btnJump_Click(sender As Object, e As EventArgs) Handles btnJump.Click
Me.tmrJump.Interval = 100
Me.tmrJump.Start()
End Sub
Private Sub tmrJump_Tick(sender As Object, e As EventArgs) Handles tmrJump.Tick
Static intOldTop As Integer
Static intCounter As Integer
Dim intStep As Integer = 5
If intCounter = 0 Then intOldTop = picSpaceRunner.Top
intCounter += 1
Select Case intCounter
Case 1 To 15
picSpaceRunner.Top -= intStep
Case 16 To 30
picSpaceRunner.Top += intStep
Case Else
picSpaceRunner.Top = intOldTop
intCounter = 0
tmrJump.Stop()
End Select
End Sub
End Class
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
If AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsMediaEnded Then
ListBox1.SelectedIndex += 1
AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem
AxWindowsMediaPlayer1.Ctlcontrols.play()
End If
It changes the index of the listbox, but it doesn't play the next file.
The file exist and there's no problem playing it the normal way.
Does anybody know what's wrong?
Solution is must to add timer"
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Dim item As Integer
item = ListBox1.SelectedIndex
If Form1.AxWindowsMediaPlayer1.playState = WMPPlayState.wmppsStopped Then
Me.ListBox1.SelectedIndex = item + 1
Form1.AxWindowsMediaPlayer1.URL = FileUrls(ListBox1.SelectedIndex)
Timer1.Start()
Else
Timer1.Start()
End If
end sub
also the following code work . first add timer to playlist and past following code
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Form1.AxWindowsMediaPlayer1.playState = WMPPlayState.wmppsStopped Then
If ListBox1.SelectedIndex < ListBox1.Items.Count - 1 Then
ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1
form1.axwindowmediaplayer1.url=listbox1.selectedindex
Else
'do nothing if no item to play
End If
End If
End Sub
I have a case where i need to generate millions of unique codes. For this I have created a generate function where the random number is generated. I call this function from a for loop and add the generated number on a list box. my code is as follow
for i=1 to val(txtnumber.txt)
mynum=generate()
next
I have created a lable on form where i wanted to display the no of secs elapsed while processing the loop. I used timer control as
timer1.start()
for i=1 to val(txtnumber.text)
mynum=generate()
listbox1.items.add(mynum)
next
timer1.stop
and on timer1_tick function
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = Val(Label1.Text) + 1
End Sub
but when i click generate button, all numbers are generated, but timer doesnot shows time elapsed.
I may have missed something, so please help me out
This is probably best handled in a BackgroundWorker. Place one on the form and set its WorkerReportsProgress=True. Also, placing a million numbers in a ListBox probably isn't a good idea, so I omitted that.
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Button1.Enabled = False
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim started As DateTime = Now
For i As Integer = 1 To val(txtnumber.txt)
mynum=generate()
BackgroundWorker1.ReportProgress(i, Nothing)
Next
Dim ended As TimeSpan = Now.Subtract(started)
BackgroundWorker1.ReportProgress(0, ended.TotalSeconds.ToString)
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
If e.UserState IsNot Nothing Then
Label1.Text = e.UserState.ToString()
Else
Label1.Text = e.ProgressPercentage.ToString
End If
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Button1.Enabled = True
End Sub
Your label should be updating correctly when the worker reports the ProgressChanged event.
What you're encountering is a threading issue. The work you are doing to generate the numbers is being executing by the UI thread, so it never gets a chance to update the screen. Take a look here: How to prevent UI from freezing during lengthy process?
This one might also have good information for you: Updating UI from another thread
Try this:
Private _Counter As Integer = 0
Private _StartTime As Date = Now
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
_StartTime = Now
_Counter = CInt(Val(txtnumber.Text))
ListBox1.Items.Clear()
Label1.Text = "0"
Timer1.Interval = 50
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
ListBox1.Items.Add(generate())
Label1.Text = New Date((Now - _StartTime).Ticks).ToString("HH:mm:ss.ff")
_Counter -= 1
If (_Counter <= 0) Then
Timer1.Stop()
End If
End Sub
Or you can research actual Threading.