Jimmy Zhang
Jimmy Zhang

Reputation: 1

Progress Bar increments at twice the intended speed each time

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.

Upvotes: 0

Views: 317

Answers (2)

A Friend
A Friend

Reputation: 2750

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

Upvotes: 1

Shadow Fiend
Shadow Fiend

Reputation: 351

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

Upvotes: 0

Related Questions