Jacob
Jacob

Reputation: 1

Using progress bars in vb to show a specific time frame

I want to use progress bars for my project throughout, since it is very much necessary, for the main part of this game I am creating, however, to get a GOOD grade at A Level, you need to be able to show variation and to not have a large amount of data replication, is there any, remotely easy understandable way to allow for a button when pressed to allow for the progress bar to be completed for me in a 5 second time period. Please let me know. If you need any code It may be rather extensive a long, because I went a really unorthodox way about this originally. But just don't really want to have about 15-20 timers in the end product.

 Private Sub ButtonClick2_Click(sender As Object, e As EventArgs) Handles ButtonClick2.Click

        money = money + (4 * LevelMultiplier2)
        label_avail_money.Text = Math.Round(money, 2).ToString("N2")
Public Class Form1
 Dim money As Decimal = 0
 Dim LevelMultiplier2 As Decimal = 1

Basically this is what is for this button, all I need for is for 1. For the Calculation to be ran 5 seconds prior to when the button is pressed and also to have a progress bar running simultaneously with the button press too. Hope this helps, also putting this into some form of code now may help me more to resolve this issue :)

Upvotes: 0

Views: 277

Answers (1)

Enigmativity
Enigmativity

Reputation: 117029

You should use Microsoft's Reactive Framework (aka Rx) - NuGet System.Reactive.Windows.Forms and add Imports System.Reactive.Linq - then you can do this:

Private Sub ButtonClick2_Click(sender As Object, e As EventArgs) Handles ButtonClick2.Click

    ButtonClick2.Enabled = False

    Observable _
        .Interval(TimeSpan.FromSeconds(5.0 / 100.0)) _
        .Take(100) _
        .ObserveOn(Me) _
        .Subscribe(
            Sub(x) ProgressBar1.Value = x + 1,
            Sub()
                money = money + (4 * LevelMultiplier2)
                label_avail_money.Text = Math.Round(money, 2).ToString("N2")
                ButtonClick2.Enabled = True
            End Sub)
End Sub

That code nicely animates the progress bar and then fills in the label_avail_money.Text value.

Upvotes: 1

Related Questions