user3438681
user3438681

Reputation: 11

Message Box showing multiple times when progress bar complete in VB.NET

I am trying some with progress bar, but it's not showing popup correctly. When i use msgbox, its appears 100s of times and when I use form2 by replacing msgbox it keep showing even I close it.

Public Class Form1
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ProgressBar1.Increment(1)
        If ProgressBar1.Value = ProgressBar1.Maximum Then
            MsgBox("Done")
        End If
    End Sub
End Class

Upvotes: 1

Views: 2014

Answers (2)

user3972104
user3972104

Reputation:

This is because you are not disable or Stop the timer. when the ProgressBar1.Value reaches the maximum the message box will display as "Done" but timer is still executing so you will get the message till timer is disabled, since the condition If ProgressBar1.Value = ProgressBar1.Maximum Then is true. so you need to disable the timer if the condition is true.

If ProgressBar1.Value = ProgressBar1.Maximum Then
  Timer1.Enabled = False
  MsgBox("Done")
End If

or you can use Timer1.Stop()

Upvotes: 0

prem
prem

Reputation: 3548

If you want to show message only once then stop the timer before the message box

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    ProgressBar1.Increment(1)
    If ProgressBar1.Value = ProgressBar1.Maximum Then
      Timer1.Stop()
      MsgBox("Done")
    End If
End Sub

Upvotes: 1

Related Questions