Click here to Skip to main content
15,606,838 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have sample code below, to use with progress bar. I need to display progress with each item of loop (percentage and bar both)

For i = 0 To 200000

I have tried everything (background workers etc.) but could not figure it out.

I request someone to write me complete code (backgroundworkder, Cross Threading and whatever) and help me make it run.

Thanks a lot in advance.
[no name] 23-May-15 12:01pm    
If you really think that after 5 years of being a member here that people are here to write your code for you, you are mistaken.

1 solution

As you are working in the UI anyway why use a backgroundworker - just update the progress bar within the loop.

This CP article shows how to display text (percentage complete) "on" the progress bar - Add the Percent (or Any Text) into a Standard Progress Bar Control[^]

As the article is in C# I've converted it for you
Private Sub UpdateProgressBar(ByVal percent As Integer)

    Dim percentText As String = percent.ToString() + "%"
    Dim font As Font = SystemFonts.DefaultFont
    ProgressBar1.Value = percent

    Using gr = ProgressBar1.CreateGraphics()
        Dim X As Single = ProgressBar1.Width / 2 - (gr.MeasureString(percentText, font).Width / 2.0F)
        Dim Y As Single = ProgressBar1.Height / 2 - (gr.MeasureString(percentText, font).Height / 2.0F)
        gr.DrawString(percentText, font, Brushes.Black, New PointF(X, Y))
    End Using
End Sub
I tested my conversion using a standard ProgressBar control(ProgressBar1 above) using the following code

Dim maxValue = 200000
Dim displayInterval = 5000
ProgressBar1.Maximum = 100

For i = 0 To maxValue
    If i Mod displayInterval = 0 Then
        UpdateProgressBar(i * 100 \ maxValue)
    End If
Note that I don't change the display for every single loop - there is not much point - see displayInterval above. Play about with the value until you get the effect you desire
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900