silly progress bar question

A

Agnes

I need to loop a datatable to do some calculation.
I want to use progress bar to show the 'process status'
Must I need to know the datatable size first ? , if there is 1000 records
inside,
I need to set the maximumn of progress bar = 1000 ??
thanks
 
L

Lucas Tam

Must I need to know the datatable size first ? , if there is 1000 records
inside,
I need to set the maximumn of progress bar = 1000 ??

Yes if you want an accurate progress bar.
 
C

Cor Ligthert

No,

Depends for the steps you take, when you do a step for every 100 it can as
well be 10.

Cor
 
H

Herfried K. Wagner [MVP]

* "Agnes said:
I need to loop a datatable to do some calculation.
I want to use progress bar to show the 'process status'
Must I need to know the datatable size first ? , if there is 1000 records
inside,
I need to set the maximumn of progress bar = 1000 ??

Yes. Notice that updating the progressbar in every iteration may cause
a lot of drawing overhead in the UI, so updating it every n iterations
may be a better approach ('If LoopCounter Mod 100 = 0 Then
UpdateProgressBar(LoopCounter)', for example).
 
C

Cablewizard

Here is how I like to do it.
Always keep the progress bar a max 100 for percent.
Then in your code, determine the number of records.
Set a variable to keep track of the percentage complete.
Set up a trigger for each percent tick.
Then update the progress bar only when you increment the trigger.
For example:

Dim numItems as Integer
Dim thisItem as Integer
Dim pctTrigger as Integer
Dim thisPct as Integer

numItems = 'Get total number of Items
'Of course, make sure numItems > 0

For ...Item, Do, Loop, etc
'Do work ...
thisItem += 1
thisPct = (thisItem * 100) \ numItems
if thisPct >= pctTrigger then
Call UpdateProgress(thisPct)
Application.DoEvents
pctTrigger += 1
end if
Next item

This way, you only update the progress when necessary.
If you have 100 items, then you get 100 updates.
If you have 1,000,000 items, then you still only get 100 updates (once every
10,000).
Although if you have that many items, you might add an additional rollover
counter for the DoEvents so it happens say every 100 items. That and you would
proly want worker threads anyway.
Pretty much works no matter how many items you have. Since it is an Integer, to
prevent overflows your number of items should be less than about 21.5 million.
But if you have that many to process, then you should be using different methods
anyway.

Gerald
 
J

Jay B. Harlow [MVP - Outlook]

Agnes,
In addition to the other comments:
Must I need to know the datatable size first ?

Is this a problem? You can use DataTable.Rows.Count to get the size of the
DataTable.

As the others suggest I would consider only updating the progress bar for
every 100 elements...

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top