How to fill a progress bar by clicking in VB.NET?

3.3k views Asked by At

I'm making a game in VB.Net and I'm not familiar with the progress bar. I need something where the player need to press a button as fast as they can to fill up the progress bar and proceed to the next leve or if not fast enough then lose.I have no code for this because I don't know how to build up something like this. Any help would be grate. Thanks

1

There are 1 answers

7
Feign On BEST ANSWER

Say you have a button, Button1, and you have a progressbar, ProgressBar1.

You can add to the progressbar's value everytime you click on Button1 by using the following code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  If ProgressBar1.Value + 1 < ProgressBar1.Maximum Then
    ProgressBar1.Value += 1
  End If
End Sub

Now, notice the condition I wrapped the increment code with. This will make sure that the user does not surpass the maximum value allowed in the progressbar1.

Edit:

As for the rest of your program, you will need to use a timer in order to track the time.

for the proceed button, you will need to use the visible property which exists on buttons in order to hide a button until some condition is met.

Re-Edit:

Public Class Form1
Private intCurrentTime As Integer = 0

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  If ProgressBar1.Value + 1 < ProgressBar1.Maximum Then
    ProgressBar1.Value += 1
  End If
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
  If intCurrentTime = 10 Then

    If ProgressBar1.Value = ProgressBar1.Maximum Then
      'SHOW "Proceed" BUTTON
    Else
      MsgBox("You have failed!")
    End If

    intCurrentTime = 0

  Else
    intCurrentTime += 1
  End If

End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  Timer1.Start()
End Sub

End Class