Progress Bar increments at twice the intended speed each time - vb.net

Private Time As New Timer
Private Sub btnWood_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWood.Click
prgWood.Value = 0
Time.Interval = 1000
Time.Start()
AddHandler Time.Tick, AddressOf IncreaseProgressBar
If prgWood.Value <> prgWood.Maximum Then
btnWood.Enabled = False
End If
Dim intAmountofWood As Integer = 11 * Rnd() + 10
intWood = intWood + intAmountofWood
Me.lblWoodAmount.Text = intWood
Private Sub IncreaseProgressBar(ByVal sender As Object, ByVal e As EventArgs)
prgWood.Increment(10)
If prgWood.Value = prgWood.Maximum Then
prgWood.Increment(0)
Time.Stop()
btnWood.Enabled = True
End If
End Sub
For my progress bar, I use a Timer to increment the value by 10 every 1 second. When I debug the project, it works fine the first time (taking 10 seconds for the progress bar to complete) but when I click the button a second time, it only takes 5 seconds, then less and less each time. This code is for an incremental game I'm trying to make for school.

From LarsTech's comment:
Public Class Form1
Private Time As New Timer
Public Sub New()
'Initialisation, etc
AddHandler Time.Tick, AddressOf IncreaseProgressBar
End Sub
'Other methods, etc
End Class
Then you need to remove the AddHandler from the button
click event

Can you do this?
Create a sub and add this code
`
ProgressBar1.Value = e.ProgressPercentage
If ProgressBar1.Value = ProgressBar1.Maximum Then
ProgressBar1.Value = ProgressBar1.Minimum
End If
`
then call that sub here.
`Private Sub btnWood_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWood.Click
'**Name of the Sub**
prgWood.Value = 0
Time.Interval = 1000
Time.Start()
AddHandler Time.Tick, AddressOf IncreaseProgressBar
If prgWood.Value <> prgWood.Maximum Then
btnWood.Enabled = False
End If
Dim intAmountofWood As Integer = 11 * Rnd() + 10
intWood = intWood + intAmountofWood
Me.lblWoodAmount.Text = intWood
End Sub
`
let see if that one works

Related

'stop' & 'start' is not a member of System.Windows.Forms.Timer vb.net

I am trying to start and stop a timer and it keeps telling me that they are both not a member of System.Windows.Forms.Timer
Below is my code. I used this in a different windows project and it works fine, don't know if I have to initialize it differently in this project.
Private Sub InitializeComponent()
Me.timer = New System.Windows.Forms.Timer
End Sub
Private Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles timer.Tick
counter -= 1
If counter = 0 Then
timer.Stop()
lblPrice.Text = "$0.00"
lblBarcode.Text = "Place Item Near Scanner"
lblName.Text = ""
lblID.Text = ""
lblUnit.Text = "Price Checker"
counter = 8
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
timer = New System.Windows.Forms.Timer
AddHandler timer.Tick, AddressOf Me.timer_Tick
timer.Interval = 1000
timer.Start()
End Sub

How can I make a button move up in size (by going vertically up), but not in total size?

I'm having a slight conflict with a button which I've been working with in Visual Basic NET.
My first code sample is for my Button_Height_Tick, which controls changing the button's height:
Dim ChangeHeight As Boolean = False
Private Sub Button_Height_Tick(sender As Object, e As EventArgs) Handles Button_Height.Tick
If Not ChangeHeight Then
Do Until FlatButton1.Height = 63
FlatButton1.Height += 1
System.Threading.Thread.Sleep(1)
Loop
ChangeHeight = True
Else
End If
End Sub
And for my FlatButton1_MouseHover.
Private Sub FlatButton1_MouseHover(sender As Object, e As EventArgs) Handles FlatButton1.MouseHover
Button_Height.Enabled = True
Button_Height.Start()
End Sub
Now, as you can see in the Button_Height_Tick sub, the code changes the height of the button to 63, however, when this code is ran, the buttons total height is changed.
Here are some photos in-case I haven't explained it well.
What my original button looks like
What I want it to do
What it's doing (going up in size vertically going down, when I want it to go up)
Please comment below if you don't understand this question.
You need to change the 'Top' position and also I notice you have a timer then just go in a do a loop. In your example there's no need for a timer.
I'll give an example using a timer and hopefully you'll understand it and can use it for what you want. I've changed 'hover' to 'enter' and 'leave'.
If it's too slow just change the increment amount.
Dim ChangeHeight As Boolean = False
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If ChangeHeight Then
FlatButton1.Height += 2
FlatButton1.Top -= 2
If FlatButton1.Height < 63 Then Exit Sub
FlatButton1.Height = 63
Timer1.Enabled = False
Else
FlatButton1.Height -= 2
FlatButton1.Top += 2
If FlatButton1.Height > 31 Then Exit Sub
FlatButton1.Height = 31
Timer1.Enabled = False
End If
End Sub
Private Sub FlatButton1_MouseEnter(sender As Object, e As EventArgs) Handles FlatButton1.MouseEnter
ChangeHeight = True
If Timer1.Enabled Then Exit Sub
Timer1.Enabled = True
Timer1.Start()
End Sub
Private Sub FlatButton1_MouseLeave(sender As Object, e As EventArgs) Handles FlatButton1.MouseLeave
ChangeHeight = False
If Timer1.Enabled Then Exit Sub
Timer1.Enabled = True
Timer1.Start()
End Sub
Hello and welcome to StackOverflow. I did a little example of how to achieve what you are looking for.
Code:
Public Class Form1
Dim buttonXCoordinate As Integer
Dim buttonYCoordinate As Integer
Dim buttonOriginalHeight As Integer
Dim buttonOriginalLocation As Point
Private Sub GetButtonCoordinate()
buttonXCoordinate = testBtn.Left
buttonYCoordinate = testBtn.Top
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
buttonOriginalHeight = testBtn.Height
buttonOriginalLocation = testBtn.Location
GetButtonCoordinate()
End Sub
Private Sub testBtn_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles testBtn.MouseEnter
Dim buttonLocation As Point = Nothing
GetButtonCoordinate()
buttonLocation.X += buttonXCoordinate
buttonLocation.Y += buttonYCoordinate - buttonOriginalHeight
testBtn.Height += buttonOriginalHeight
testBtn.Location = buttonLocation
End Sub
Private Sub testBtn_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles testBtn.MouseLeave
testBtn.Height = buttonOriginalHeight
testBtn.Location = buttonOriginalLocation
End Sub
End Class
I did it really fast but it's enough to give you an idea to how to achive your goal.
In my example there is a button called testBtn, when you go over it with the mouse it the button's height is increased and it returns back to normal when you move your mouse out of it

Create timers dynamically and add unique names VB.net

Alright,
I know how to create buttons and give them each unique names to access them
I do it like this
Dim btnName As String
Dim x As Short
For i As Short = 1 To 3
btnName = "button" & CStr(i)
x += 3
Dim button1 As New Button
button1.Name = btnName
Me.Controls.Add(button1)
button1.Location = New Point(10, x * 10)
button1.Text = "Hello" & i
Next
When I try to create a timer, I cannot give it a name like I did above with the buttons
btnName = "button" & CStr(i)
button1.Name = btnName
So I don't know how to access them and/or activate them for instance. I want to create like three timers and name them like "timer1", "timer2", "timer3"
How do I achieve that?
'Here Is a form code that starts a timer on a button click
Public Class Form1
Dim t1 As Timer
Dim t2 As Timer
Dim t3 As Timer
Private Sub btnT1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnT1.Click
'on btn click start timer1
t1 = New Timer
t1.Tag = DateTime.Now
AddHandler t1.Tick, AddressOf MyTickHandler
t1.Start()
End Sub
Private Sub btnT2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnT2.Click
'on btn click start timer2
t2 = New Timer
t2.Tag = DateTime.Now
AddHandler t2.Tick, AddressOf MyTickHandler
t2.Start()
End Sub
Private Sub btnT2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnT3.Click
'on btn click start timer3
t3 = New Timer
t3.Tag = DateTime.Now
AddHandler t3.Tick, AddressOf MyTickHandler
t3.Start()
End Sub
Sub MyTickHandler(ByVal sender As Object, ByVal e As EventArgs)
dim t As Timer = DirectCast(sender, Timer)
dim timerString = "The timer started at " & t.Tag.ToString & " just ticked..."
End Sub
Private Sub btnStopT1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopT1.Click
'stop timer1
t1.Stop()
t1.Dispose()
End Sub
Private Sub btnStopT2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopT2.Click
'stop timer 2
t2.Stop()
t2.Dispose()
End Sub
Private Sub btnStopT3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopT3.Click
'stop timer 3
t3.Stop()
t3.Dispose()
End Sub
End Class
Timer Class
This tutorial should help you create a timer object.
Tutorial 2: Create a Timed Math Quiz
Tutorial link
In detail step 3 will do the trick.
Step 3: Add a Countdown Timer
Link to step 3
This piece of shows how you can do something with the tick event (copy from the MSDN site)
Private Sub Timer1_Tick() Handles Timer1.Tick
If timeLeft > 0 Then
' Display the new time left
' by updating the Time Left label.
timeLeft -= 1
timeLabel.Text = timeLeft & " seconds"
Else
' If the user ran out of time, stop the timer, show
' a MessageBox, and fill in the answers.
Timer1.Stop()
timeLabel.Text = "Time's up!"
MessageBox.Show("You didn't finish in time.", "Sorry!")
sum.Value = addend1 + addend2
startButton.Enabled = True
End If

Count down timer in vb.net

In the following code I have a timer that counts down from 5 mins. I am trying to have a visual count down timer in a lbl in mm:ss but the example I used doesn't work. It counts down but doesn't update the lbl until it hits 00:00.
The asker of the following question (were I got the code) said it works perfectly but for me it doesn't at all.
The Example I used
My code:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
picLogo.SizeMode = PictureBoxSizeMode.StretchImage
'Timer until update
tmrUpdate.Interval = 300000 '5 minutes
TargetDT = DateTime.Now.Add(CountDownFrom)
tmrUpdate.Enabled = True
End Sub
Private Sub tmrUpdate_Tick(sender As Object, e As EventArgs) Handles tmrUpdate.Tick
Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
lblTimer.Text = ts.ToString("mm\:ss")
Else
lblTimer.Text = "00:00"
tmrUpdate.Stop()
End If
End Sub
Answer:
Using a Async Sub I had the count down timer running while other stuff was going on in the back ground. This way the app could still be used during the Sub Wait() and this code also displayed the count down timer.
Used one timer on a 1 sec interval.
Private Async Sub DoStuff()
'Doing stuff
timeUpDate = 599
tmrUpdate.Start()
Application.DoEvents()
Await Task.Run(Sub()
Wait()
End Sub)
Loop
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles tmrUpdate.Tick
Dim hms = TimeSpan.FromSeconds(timeUpDate)
Dim m = hms.Minutes.ToString
Dim s = hms.Seconds.ToString
If timeUpDate > 0 Then
timeUpDate -= 1
lblTimer.Text = (m & ":" & s)
Else
tmrUpdate.Stop()
lblTimer.Text = "text"
End If
End Sub
Private Sub Wait()
Threading.Thread.Sleep(600000)
End Sub

How to create multiple timer handler in vb.net

I have to create multiple timer "n" times with handler.
Which will be stored for a row in my DataGrid.
For each row there will be a timer that works seperately.
What I thought of looks like:
Private Sub CreateTimer()
Dim tmr As New Timer
tmr.Interval = 1000 '1 Second
tmr.Enabled = True
AddHandler tmr.Tick, AddressOf GlobalTimerTick
End Sub
'A timer tick handler that would work for each timer I add with the sub above
'All timers I created should work seperately
Private Sub GlobalTimerTick(TheTimer as Timer, ByVal sender As Object, ByVal e As EventArgs)
mynumber = mynumber + 1
With DataGridView1
.Rows(n).Cells(4).Value = mynumber " saniye"
End With
End Sub
So how can I achieve this?
I believe Tag property of Timer would work beautifully in your case. I don't have my IDE currently, but the following snippet should give you the idea.
Private Sub CreateTimer()
Dim tmr As New Timer
tmr.Interval = 1000 '1 Second
tmr.Enabled = True
tmr.Tag = ROW_INDEX
AddHandler tmr.Tick, AddressOf GlobalTimerTick
End Sub
'A timer tick handler that would work for each timer I add with the sub above
'All timers I created should work seperately
Private Sub GlobalTimerTick(ByVal sender As Object, ByVal e As EventArgs)
mynumber = sender.Tag
With DataGridView1
.Rows(n).Cells(4).Value = mynumber " saniye"
End With
End Sub