Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Progress Bar

Status
Not open for further replies.

Mighty

Programmer
Feb 22, 2001
1,682
US
I have a form which has a label and a progress bar. When the user clicks on a button to retrieve data from an external source, I want to display this form. When the main form returns a value I want to hide the form. In theory this should be very simple, but I am having issues getting the progress bar to update. I am trying to keep it simple but just increasing the value of the bar until it gets to 100 and then starting again - until the form is closed. Code is below:

Code:
Public Class ProgressBar

    Private Sub ProgressBar_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Set ProgressBar Range
        Me.ProgressBar1.Minimum = 0
        Me.ProgressBar1.Maximum = 100
        Me.ProgressBar1.Value = 0

        ' Call function to update it
        Call UpdateProgress()

    End Sub


    Private Sub UpdateProgress()

        ' Keep the progress bar rotating
        While True

            If Me.ProgressBar1.Value < 100 Then
                Me.ProgressBar1.Value += 5
            Else
                Me.ProgressBar1.Value = 0
            End If

        End While

    End Sub

End Class

If I leave out the code to update the value, the form displays but the progress bar doesn't move. If I include it, the form doesn't even display.

Any advice?

Sorry about all the questions but I'm new to this.

Mighty
 
While I try to avoid using it, Application.DoEvents() may be what you need in the loop to get everything to paint like you want. Be advised, it can slow down the processing.

--------------------------------------------------
“Crash programs fail because they are based on the theory that, with nine women pregnant, you can get a baby a month.” --Wernher von Braun
--------------------------------------------------
 
Another way that might be simpler is to refresh the form each time the progress bar increments. That is the solution I chose.
 
bakershawnm,

Can you please elaborate on how to do this. When I refresh the form the progressbar goes back to the start. I even tried to increment a simple counter in a label to see if I was doing something wrong with the progress bar and it never gets past 2.

See the following code:

Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        If Me.ProgressBar1.Value < 100 Then
            Me.ProgressBar1.Value += 5
            Label2.Text = CInt(Label2.Text) + 1
        Else
            Me.ProgressBar1.Value = 0
        End If

        Me.Refresh()

    End Sub

Mighty
 
Actually got it working using a timer:

Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Set the timer interval and enable
        Me.Timer1.Interval = 20
        Me.Timer1.Enabled = True

    End Sub


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

        ' Increment the progress bar
        Me.ProgressBar1.Value += 5

        ' If we have reached the maximum go back to the start
        If Me.ProgressBar1.Value >= Me.ProgressBar1.Maximum Then
            Me.ProgressBar1.Value = 0
        End If

    End Sub

Mighty
 
Mighty,

First let me say that I am multi threading. The form with the progress bars is the main form and the threads raise an event back to the main form to increment the progress bar. Here is an event handler.

Private Sub WIRSPrgrsBarEvntHndlr()
If Me.WIRSBar.Value >= WIRSBar.Maximum Then
Me.WIRSBar.Value = 1
End If
Me.WIRSBar.PerformStep()
Me.Refresh()
End Sub

It is 'invoked' when the thread raises the event.

Public Sub PrgrsStepMthd(ByVal bar As Integer) _
Handles MWPrsr.Prgrsbar, CPrsr.Prgrsbar, BTPrsr.Prgrsbar, MWTPrsr.Prgrsbar, _
FOPrsr.Prgrsbar, SILSPrsr.Prgrsbar, MkByPrsr.Prgrsbar, BAIPrsr.Prgrsbar, _
StkrPrsr.Prgrsbar, TestPrsr.Prgrsbar, UDIS.Prgrsbar, WIRS.Prgrsbar
Select Case bar
Case 1
If Me.MWBar.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf MWPrgrsBarEvntHndlr))
End If
Case 2
If Me.CoderBar.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf CoderPrgrsBarEvntHndlr))
End If
Case 3
If Me.BtTgBar.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf BtTgPrgrsBarEvntHndlr))
End If
Case 4
If Me.MWTBar.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf MWTPrgrsBarEvntHndlr))
End If
Case 5
If Me.FOBar.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf FOPrgrsBarEvntHndlr))
End If
Case 6
If Me.SILSBar.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf SILSPrgrsBarEvntHndlr))
End If
Case 7
If Me.MkByBar.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf MkByPrgrsBarEvntHndlr))
End If
Case 8
If Me.BAIBar.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf BAIPrgrsBarEvntHndlr))
End If
Case 9
If Me.STKBar.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf STKPrgrsBarEvntHndlr))
End If
Case 10
If Me.TestBar.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf TestPrgrsBarEvntHndlr))
End If
Case 12
If Me.UDISBar.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf UDISPrgrsBarEvntHndlr))
End If
Case 13
If Me.WIRSBar.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf WIRSPrgrsBarEvntHndlr))
End If
End Select
End Sub

I also update a label above the appropriate progress bar when necessary by raising an event from the thread.

Private Sub WIRSLabelEvntHndlr()
Me.WRSLabel.Text = lbltxt
Me.Refresh()
End Sub

This is 'invoked' by:

Public Sub LblHndlrMthd(ByVal lbl As Integer, ByVal lbltext As String) _
Handles MWPrsr.UpdtLbl, CPrsr.UpdtLbl, BTPrsr.UpdtLbl, MWTPrsr.UpdtLbl, _
FOPrsr.UpdtLbl, SILSPrsr.UpdtLbl, MkByPrsr.UpdtLbl, BAIPrsr.UpdtLbl, _
StkrPrsr.UpdtLbl, TestPrsr.UpdtLbl, WIRS.UpdtLbl
lbltxt = lbltext
Select Case lbl
Case 1
If Me.MWLabel.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf MWLabelEvntHndlr))
End If
Case 2
If Me.CoderLabel.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf CoderLabelEvntHndlr))
End If
Case 3
If Me.BtTgLabel.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf BtTgLabelEvntHndlr))
End If
Case 4
If Me.MWTLabel.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf MWTLabelEvntHndlr))
End If
Case 5
If Me.FOLabel.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf FOLabelEvntHndlr))
End If
Case 6
If Me.SILSLabel.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf SILSLabelEvntHndlr))
End If
Case 7
If Me.MkByLabel.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf MkByLabelEvntHndlr))
End If
Case 8
If Me.BAILabel.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf BAILabelEvntHndlr))
End If
Case 9
If Me.STKLabel.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf STKLabelEvntHndlr))
End If
Case 10
If Me.TstFrmLbl.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf TstFrmLabelEvntHndlr))
End If
Case 11
If Me.TstToLbl.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf TstToLabelEvntHndlr))
End If
Case 13
If Me.WRSLabel.InvokeRequired = True Then
Me.Invoke(New EventHandler(AddressOf WIRSLabelEvntHndlr))
End If
End Select
End Sub


Here is the thread specific information

Public Event ClrPrgrsbar(ByVal Bar As Integer)
Public Event UpdtLbl(ByVal lbl As Integer, ByVal lblinfo As String)

RaiseEvent UpdtLbl(9, "Extracting Data From " & infile.Name)
RaiseEvent Prgrsbar(9)

My threads are classes instantiated in the main form

Dim thrdWIRS As System.Threading.Thread
Dim WithEvents WIRS As New CL_ExtrctWIRS()

and spawned

thrdWIRS = New System.Threading.Thread(AddressOf WIRS.Parser)
thrdWIRS.Start()


This might be a little more information than you were looking for but I hope it helps.

It took a lot of learning and searching to get to what I have here since I had no clue what I was doing when I started. :)

Shawn

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top