Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

dynamically change progress bar

Posted on 2006-11-03
Last Modified: 2010-04-16
Hi the below code should ensure that my progress bar on the relevent import form accurately shows the progress of my import and that is my problem - getting the progress bar to accurately show the progress of the import. Please help!

public override bool Import ( string filename, params object[] additionalParameters )
                  for ( int i = 0; i < 100; i ++ ) //This is the problem line because there could be any number of records

                        // Ensure it hasn't been cancelled
                        if ( !this._cancelled )
                              this.Raise_ProgressUpdated ( "Importing", "Client " + i.ToString (), i );//This will show "Client" + record number
                                                                        //Do import here
                              System.Threading.Thread.Sleep ( 50 );
                              this.Raise_Complete ( false );
                              return false;


                  this.Raise_Complete ( true );

                  return true;

Question by:JakeyCakes
  • 2
  • 2

Expert Comment

ID: 17865401
Loop through the file first to get the correct number of rows, and then set the maximum of your progressbar:

public int NumberOfLines(string path)
      int num = 0;
      using (StreamReader sr = new StreamReader(path, Encoding.Default)) {
            while (sr.ReadLine() != null) {
      return num;
LVL 15

Accepted Solution

ozymandias earned 250 total points
ID: 17865418
You first have to check the number of records

    int n = number_of_records;
    // here make a callback or raise an event to set the correct max value in the progress bar


for (int i =0; i < n; i++)

LVL 15

Expert Comment

ID: 17865421
sorry, didn't see redpipe's post

Assisted Solution

redpipe earned 250 total points
ID: 17865453
Another approach is to check if the maximum of the progressbar is reached and adjust it accordingly

using (StreamReader sr = new StreamReader(path, Encoding.Default)) {
  if ( !_cancelled )
    if(YourProgressBar.Value >= YourProgressBar.Maximum){
      //Choose an appropriate value for dynamic increase. Could also be 10% or whatever that suites your purpose
      YourProgressBar.Maximum = YourProgressBar.Maximum + 100;
      // => Continue doing your magic  :-)
  } else {
  // When your done importing or the user has cancelled. Set the progressbar to 100% or hide it, to indicate that your done
  YourProgressBar.Value = YourProgressBar.Maximum;

Author Comment

ID: 17866286
Thank you both for your help, I am going to split the points evenly.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSRS subreport with parameter 3 47
jQuery - following an example but can I save data server side? 20 58
.NET 2008 VB and C# 6 39
Sort GridView by ID Descending 1 17
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question