Progress Bar How to show progress untill event is complete

vbMarkO

Centurion
Joined
Aug 19, 2005
Messages
157
How do I do this

I want to add a progress Bar to my app and I want it to show the progress of an event that is triggered..... how would this be done?

NOTE never done a progress bar before wouldnt even know where to start

vbMarkO
 
Generally you use a progress bar by counting something.

You set the progress bar max value with ProgressBar.Maximum. In order to make progress increment, you would use the ProgressBar.Incriment(int) method where the int is the amount you want to advance the progress bar by. See the MSDN for more informaiton an all this.

The simplest example is reading in a bunch of files. Say your program needs to read in a bunch of files from a folder and do something to them. To use a progress bar to show progress in this situation I would read in all the file names and store them in an array (you have to know the maximum value of the pbar so it will incriment properly), set my progressbar.maximum to the size of the array, and then iterate through the array incrimenting the progress bar by 1 after I finish processing the file.
Visual Basic:
 'psuedo code-esque
Dim fileNames as string() = Directory.GetAllFiles(someFolder) 'Directory.GetAllFiles it isn't exactly right but it's close
pbar.Maximum = fileNames.Length

for each fileName as string in fileNames
   ProcessFile(fileName) ''do what you would do that takes so long here
   pbar.Incriment(1)
next

pbar.Value = pbar.Maximum 'make sure it goes all the way to the end
Of course, things get tricky if you don't have anything to count for the progress bar.
 
Hmmm
This doesnt seem so complicated if as you say you items to count....

However, I am not sure how this might work in my case, and thus a fear of its going to get complicated has just come all over me LOL

Yet, maybe not, I do have something to count, that is if you consider Lines of text something???

WHat I have is Text in an RTB that I am formatting ... the lines can be as many as 2 to 5000 lines.... so the process on some is pretty fast so the progress Bar will hardly be noticable but for most of them it can take as long 30 secs to 1 minute to complete...

What I was hoping to do is show the progress of the slower operations...

The process is like this
it formats a line the increments through until each line is formatted

not sure how I would point the Pbar toward this inremetation

Any thoughts?

Would I use the Length of the RTB for Max? Such as
Visual Basic:
For Each line As String in rtb.Lines
  ' Process Code for text formatting here
  pbar.increment(1)  '  not sure this is right just showing the idea

Next


Anythoughts appreciated

vbMarkO
 
Sounds like you can do:
Visual Basic:
        pbar.Maximum = rtb.Lines.GetLength(0)

        Dim ilp As Integer

        For ilp = 0 To rtb.Lines.GetLength(0) - 1

            'do formatting

            pbar.Value = ilp + 1
        Next ilp
 
Ok,
Could we help this at all,

I am using the Pbar and I just noticed that it is making the time to format in some cases almost double and on the bigger formats its ridiculous.

I want it to measure the time of the event but not enterfere with it so that it makes it longer.

I will post the code to show you th process maybe you might know a better way to configure it.

Visual Basic:
 'do formatting
        rtbText.Text = bibleLBL.Text & " 1" & vbCrLf & vbCrLf & rtbText.Text
        rtbText.Find(bibleLBL.Text & " 1", RichTextBoxFinds.None)
        rtbText.SelectionAlignment = HorizontalAlignment.Center
        rtbText.SelectionFont = New Font("Arial", 22, FontStyle.Bold)
        rtbText.SelectionColor = Color.Blue


        pbar.Maximum = rtbText.Lines.GetLength(0)

        Dim ilp As Integer

        For ilp = 0 To rtbText.Lines.GetLength(0) - 1






            For Each line As String In rtbText.Lines
                If line.Contains(":1") Then
                    ' Do nothing
                    'scripText.SelectAll()
                    'scripText.Find(line, RichTextBoxFinds.None)



                Else
                    rtbText.Find(line, RichTextBoxFinds.None)

                    rtbText.SelectionIndent = 18



                End If

            Next

            pbar.Value = ilp + 1

        Next ilp

        pbar.Value = 0

Any ideas?

I was thinking maybe eve a different approach.... I mean, isnt there a way to measure the time of an event How it takes to complete its task?

Seems if so, that we wouldnt want the progress Bar to intefere with the event only to measure its results.

I may be way off but thought I would throw it out there anyway.


vbMarkO
 
vbMarkO said:
...isnt there a way to measure the time of an event How it takes to complete its task?
Unfortunately, there isn't. Tasks will execute at different rates for any given computer based on CPU speed, memory, and load.

vbMarkO said:
I am using the Pbar and I just noticed that it is making the time to format in some cases almost double and on the bigger formats its ridiculous
Hmmm. That's interesting. I've never noticed a major slow down in execution time when I use a progress bar versus not using a progress bar. My experience is that it is basically a painless operation. Why do you have the double loop in your code? It looks like your processing over the RTB line times. I think you should be able to do this with one loop. Try:
Visual Basic:
 'do formatting
        rtbText.Text = bibleLBL.Text & " 1" & vbCrLf & vbCrLf & rtbText.Text
        rtbText.Find(bibleLBL.Text & " 1", RichTextBoxFinds.None)
        rtbText.SelectionAlignment = HorizontalAlignment.Center
        rtbText.SelectionFont = New Font("Arial", 22, FontStyle.Bold)
        rtbText.SelectionColor = Color.Blue

        pbar.Maximum = rtbText.Lines.GetLength(0)
        For Each line As String In rtbText.Lines
           If not line.Contains(":1") Then 'just my personal style, you can keep the other invariant if you like
              rtbText.Find(line, RichTextBoxFinds.None)
              rtbText.SelectionIndent = 18
           End If
           pbar.Incriment(1)
        Next
            

        pbar.Value = 0
 
mskeel,

Your code did the trick. I dont mean the loop I mean the
pbar.increment(1)

just to be sure I tried the other way it was written compared ot this ...

I dont know why the other made it slow down but it did.

Your however it now works like a charm.

Question though, I would like to display in a label next to the pbar the percentage done how might I do this?

shooting from the hip here would this be right?
label4.Text = pbar.value & " %"

That may not be right but then it is a guess

what are your thoughts

vbMarkO
 
Why not just display the percentage in the progressbar itself? :)

Visual Basic:
    Private Sub UpdatePBar()

        Dim x As Single
        Dim y As Single
        Dim percentage As String = CType((ProgressBar1.Value / ProgressBar1.Maximum * 100), Integer).ToString & "%"
        Dim gr As Graphics = ProgressBar1.CreateGraphics

        Dim sz As SizeF = gr.MeasureString(percentage, ProgressBar1.Font, ProgressBar1.Width)

        x = (ProgressBar1.Width / 2) - (sz.Width / 2)
        y = (ProgressBar1.Height / 2) - (sz.Height / 2)

        gr.DrawString(percentage, ProgressBar1.Font, Brushes.Black, x, y)

    End Sub
Call this every time you update the progressbar. The only downside is that you need to call the Refresh method of the progressbar right before you call this, otherwise you get ugliness. :( It's assumed this code is in the form with the progressbar and the progressbar is named ProgressBar1. Change as necessary. :)
 
vbMark0 -- the reason your code was taking so long is becuae you were looping through everything many more times than was necesary. If you want to explicitly set the value of the progress bar as you were doing originally you would do this:
Visual Basic:
 'do formatting
        rtbText.Text = bibleLBL.Text & " 1" & vbCrLf & vbCrLf & rtbText.Text
        rtbText.Find(bibleLBL.Text & " 1", RichTextBoxFinds.None)
        rtbText.SelectionAlignment = HorizontalAlignment.Center
        rtbText.SelectionFont = New Font("Arial", 22, FontStyle.Bold)
        rtbText.SelectionColor = Color.Blue
        pbar.Maximum = rtbText.Lines.GetLength(0)
        Dim ilp As Integer = 0
        For Each line As String In rtbText.Lines
           If not line.Contains(":1") Then
                   rtbText.Find(line, RichTextBoxFinds.None)
                   rtbText.SelectionIndent = 18
            End If
            ilp += 1
            pbar.Value = ilp  ''this would probably be better as a regular for loop.
        Next
        pbar.Value = 0
The way you were doing it before, you would process all the lines in the file line times ==> processingTime = numberOfLines * numberOfLines when really the processing time should only be equal to the numberOfLines (one loop). Do you see the difference? Setting the value the way you did would still work with the loop, you just have to count correctly -- you were doing too much work.

Machaira put up some solid code that will really give you progressbar a wow effect. Go with that. It shouldn't be too hard to impliment.
 
One suggestion - instead of doing:

pbar.Value = ilp

You can do:

pbar.Increment(1)

Gets rid of having a variable just to change the ProgressBar.
 
Machaira said:
Gets rid of having a variable just to change the ProgressBar.
I agree. That was my original recomendation (see post #7). I was trying to show that the slow down occured due to the use of nested loops, not becuase the value as being explicitly set instead of using the incriment method -- attempting to help answer VBMarkO, "I dont know why the other made it slow down but it did." I apologize for any confusion I might have cuased. By consensus, post #7 is the code to use.
 
Back
Top