Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Visual Basic 2008 Express Edition!
Posted

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.

 '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.

Posted

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

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

Visual Basic 2008 Express Edition!
Posted

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.

 

'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

Visual Basic 2008 Express Edition!
Posted
...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.

 

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:

 '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

Posted

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

Visual Basic 2008 Express Edition!
Posted

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

 


   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. :)

Here's what I'm up to.
Posted

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:

 '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.

Posted
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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...