VB.NET Timer stops working after it ticks the first time - vb.net

the timer in my code stops working after the first tick, I have it set to start ticking every second when the form loads:
Private Sub FormIdleTimeWaster_Load(sender As Object, e As EventArgs) Handles MyBase.Load
timerCPS.Interval = 1000
timerCPS.Start()
End Sub
but after the first tick, it stops working:
Private Sub TimerCPS_Tick(sender As Object, e As EventArgs) Handles timerCPS.Tick
lblCPS.Text = CStr(CPS)
lblTotalHW.Text = CStr(CPS + HWTotal)
End Sub
All the other code refrencing timerCPS is
Me.timerCPS = New System.Windows.Forms.Timer(Me.components)
Friend WithEvents timerCPS As Timer
Nowhere else references timerCPS in my code and I'm not sure what's wrong

I have replicated your code as given in the question and it is working perfectly fine. It is ticking over every second.
I have modified your code to this:
Public Class FormIdleTimeWaster
Private CPS As Integer = 1
Private Sub timerCPS_Tick(sender As Object, e As EventArgs) Handles timerCPS.Tick
CPS += 1
lblCPS.Text = CStr(CPS)
End Sub
Private Sub FormIdleTimeWaster_Load(sender As Object, e As EventArgs) Handles MyBase.Load
timerCPS.Interval = 1000
timerCPS.Start()
End Sub
End Class
I can clearly see the value in the label increasing by one each tick.
The call to timerCPS.Start() is not needed in timerCPS_Tick.
If you still have an issue then there is some other code you haven't shown.

Related

How continually output text to a RichTextBox with it being visually noticeable?

I am just trying to make my own VB project right now to get familiar with the language, and all I would like for it to do is continually print a string to the next line in a RichTextBox.
The issue that I can't figure out is to have it print one after another, it is printing all at once. Ill have some code down below to show where I am at right now.
I've tried using different counting methods, and depending on how it is set up, the debugger won't even load...
Friend WithEvents TableLayoutPanel1 As System.Windows.Forms.TableLayoutPanel
Friend WithEvents Button1 As System.Windows.Forms.Button
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
End Sub
Private Sub RTB1_TextChanged(sender As System.Object, e As System.EventArgs)
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim counter1 As Integer = 0
Dim i As String = "- I" & vbCrLf
While counter1 <= 10
Timer1.Interval = 1000
Timer1.Start()
i = i + i
counter1 += 1
End While
RichTextBox1.Text = i
'Loop
'Environment.NewLine
End Sub
Friend WithEvents TableLayoutPanel2 As System.Windows.Forms.TableLayoutPanel
Private Sub TableLayoutPanel2_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles TableLayoutPanel2.Paint
End Sub
Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
Private Sub RichTextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles RichTextBox1.TextChanged
RichTextBox1.SelectionStart = RichTextBox1.Text.Length
RichTextBox1.ScrollToCaret()
End Sub
Friend WithEvents Timer1 As System.Windows.Forms.Timer
Thank you to anyone that takes the time to look at this and help me out!
I really am looking for my output to scroll down the RichTextBox and continually to output a string on a new line over and over again one at a time.
As described:
Create a System.Windows.Forms.Timer. There are different types of Timers available. This is the one you need to update an UI component, since it's Tick event is raised in the UI thread.
Initialize the Timer and set its Interval to 1 second (1000 ms). The initialization is performed in the Shown() event of the Form, which is raised when the Form is ready to be presented (see the Docs).
Add the Timer.Tick event handler (here is added in code)
Initialize an Integer field (here, called timerCounter) which is incremented each time the Timer Ticks.
In the Tick event, add a line of text to the RichTextBox control using it's AppendText() method, which allows to add text to the control without clearing it. This method is common to all controls that inherit TextBoxBase.
Note:
I'm adding the text to the RichTextBox using an interpolated string $"{Some value}". If your version of VB.Net doesn't suppport it, use the older format:
RichTextBox1.AppendText("Line number " & timerCounter.ToString() & Environment.NewLine)
Private rtbTimer As System.Windows.Forms.Timer
Private timerCounter As Integer = 0
Protected Sub TimerTick(sender As Object, e As EventArgs)
timerCounter += 1
RichTextBox1.AppendText($"Line number {timerCounter} {Environment.NewLine}")
RichTextBox1.ScrollToCaret()
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
rtbTimer = New Windows.Forms.Timer With { .Interval = 1000 }
AddHandler rtbTimer.Tick, AddressOf TimerTick
rtbTimer.Start()
End Sub

How Do I total all the variables? in Vb.net

How do I add all the variables in VB.net? I do what I was instructed to increment it by placing it outside. Now that I have done it whenever I press the button the output is only 95 and whenever I click multiple times on the picturebox with the incrementation the output is still the same from last time. Nothing is really happening
Private BibimbapQuantity = 1
Private BulgogiQuantity = 1
Private BibimbapPrice As Integer = 45 * BibimbapQuantity
Private BulgogiPrice As Integer = 50 * BulgogiQuantity
Private TotalPriceInt As Integer = BibimbapPrice + BulgogiPrice
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles buttonBibimbap.Click
BibimbapQuantity += 1
Me.DataGridView2.Rows.Add("Bibimbap", "1", BibimbapPrice)
End Sub
Private Sub buttonBulgogi_Click(sender As Object, e As EventArgs) Handles buttonBulgogi.Click
BulgogiQuantity += 1
Me.DataGridView2.Rows.Add("Bulgogi", "1", BulgogiPrice)
End Sub
Private Sub totalPrice_Click(sender As Object, e As EventArgs) Handles totalPrice.Click
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
totalPrice.Text = TotalPriceInt
End Sub
You might've written the formula at the top but it's only executed once. You need to execute the formula every time.
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles buttonBibimbap.Click
BibimbapQuantity += 1
BibimbapPrice = 45 * BibimbapQuantity ' <------
Me.DataGridView2.Rows.Add("Bibimbap", "1", BibimbapPrice)
End Sub
You'll need to do the same with the total.
When you are done, put your code in https://codereview.stackexchange.com/ they will help you a lot with refactoring it. Only post on codereview when the code is actually working. They will help you find better way of writing your logic and find possible problems. Great learning opportunity.

How to do for loop inside a timer tick event vb 2015?

I would like to do a For Loop inside a timer. Since I want to check an array with element row continuously and I cant continue since the loop seems to not move from 0 on tick, is there a way around this?
I first tried this:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
For row = 0 To 9
msgbox(row)
Next
And then I tried another approach, as suggested in an answer
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
row+=1
msgbox(row)
if row = 10 then
timer1.stop()
end if
Next
MsgBox will now output 0++ on tick but does not stop at 10.
Output Picture
Dim row As Integer = -1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
RefreshData()
Timer1.Enabled = True
initialize()
End sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
increment()
MsgBox(row)
End sub
Private Sub increment()
If row > 8 Then
row = 0
Else
row = row + 1
End If
'my if else statement for checking array variable(row)
End Sub
With this code msgbox was able to output 0 to 9 and repeat the process since i need it to continuously monitor the array. For some reason though when msgbox is situated at the first line of increment method or on timer tick before increment is called the output stays -1 the whole time dunno why. Anyways thanks for all the input as im still new to visual basic 2015
Instead of using for loop use a global variable and increment it inside the tick function
Dim ctr as integer = 0
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ctr=ctr+1
row(ctr)
msgbox(row)
if ctr == 10 then
ctr = 0
timer1.stop
end if

How can I make Timer do different things based on how it was started

How can I make my Timer do different things depends on what activated it? I've tried using this code
Dim a As Integer = 0
Dim b As Integer = 0
Private Sub Button1_MouseHover(sender As Object, e As EventArgs) Handles Button1.MouseHover
Timer1.Start
End Sub
Private Sub Button2_MouseHover(sender As Object, e As EventArgs) Handles Button2.MouseHover
Timer1.Start
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick, Button2.MouseHover, Button1.MouseHover
If sender Is Button1 Then
a = a + 1
TextBox1.Text = a
End If
If sender Is Button2 Then
b = b + 1
TextBox2.Text = b
End If
End Sub
but Textbox just add 1 once. This means that the Timer just act one time not continuously like Timer usually do. So is there anything I do wrong there, or i can do something different?.
Another possibly simpler approach, would be to use the Timer.Tag property and use one handler for both buttons:
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
Timer1.Tag = sender
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
a += 1
If Timer1.Tag Is Button2 Then
TextBox1.Text = a.ToString
End If
If Timer1.Tag Is Button1 Then
TextBox2.Text = a.ToString
End If
End Sub
Since Tag is already of type Object no external casting is needed.
I included the Start function, since I wasn't sure how you're initially starting the timer. If you're doing in a different manner it can be left out of the button event handler
I am not sure what is value of this and this is not, generally speaking, something people do, but if you want to know which action activated timer you need to register it
Private _timer As Timer
Private _activatingControl As Object
Private Sub ActivateTimer(c as Object)
_activatingControl = c ' this is first
_timer.Start()
End Sub
Private Sub Button2_MouseHover(sender As Object, e As EventArgs) Handles Button2.MouseHover
ActivateTimer(sender)
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick, Button2.MouseHover, Button1.MouseHover
. . . . . .
System.Diagnostics.WriteLine("timer was activated by" + _activatingControl.ToString())
If DirectCast(_activatingControl, Control).Name = "Button1" Then
. . . .
ElseIf DirectCast(_activatingControl, Control).Name = "Button2" Then
. . . .
End If
End Sub
So, you will always need to activate timer via ActivateTimer. Timer1_Tick(sender As Object will always be Timer itself
I like tinstaafl's Tag property method. A similar approach would be to create a custom timer which inherits from System.Windows.Forms.Timer and has a Button property which can be set in the button's MouseHover event handler.
The timer's Start method only needs to be called once so I moved this to the form's load event handler. I am not sure exactly what you are wanting to achieve. Depending on this, the timer's Start method could be left where it was in the button MouseHover event handler and Timer1.Stop could be called at the end of the timer's tick event handler. This would then increment the value of the counter (a) only once in response to each MouseHover event. Alternatively Timer1.Stop could be called in the buttons' MouseLeave events if you only wanted the counter to increment while the mouse is hovering over the buttons.
Public Class Form1
Private Class CustomTimer
Inherits System.Windows.Forms.Timer
Private m_myButton As Button
Public Property Button() As Button
Get
Return m_myButton
End Get
Set(ByVal value As Button)
m_myButton = value
End Set
End Property
End Class
Private WithEvents Timer1 As New CustomTimer
Private a As Integer
Private Sub Form1_Load(sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Timer1.Interval = 100
Timer1.Start()
End Sub
Private Sub Button_MouseHover(sender As Object, e As EventArgs) Handles Button1.MouseHover, Button2.MouseHover
Timer1.Button = DirectCast(sender, Button)
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
a += 1
If Timer1.Button Is Button1 Then
TextBox1.Text = a.ToString
ElseIf Timer1.Button Is Button2 Then
TextBox2.Text = a.ToString
End If
End Sub
End Class

My first VB program, Error

I am working on a program for my brother, to show him what VB is. I had my friend help me here, but I now get an error when running on these lines.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(10)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Timer1.Tick = 1000
**Timer1.Start()**
End Sub
the **'s arent there, I was trying to bold it.
It returns me with this error:
Error 1 'Public Event Tick(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event. C:\Users\Kyle\Desktop\Form1.vb 10 9 WindowsApplication1
Tick is an Event of the timer. to set the interval use Interval property, or pass it to the constructor.
timer = New Timer(3000)
timer.Enabled = True