Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem related to progressbar control

Newbie
 
Join Date: May 2009
Location: jaipur,India
Posts: 15
#1: May 16 '09
What I am trying to figure out is how to display a progress bar when im performing a large task. This task will consist of reading records from a database, writing them to a text file, manipulating the data to write to a database and then present them on the screen. There can be several thousand records that I have to manipulate. I am having trouble getting a progressbar to accuratly display the progress. My problem seems to be that since i cant determine exactly how many records and how much time it will take that I cant accuratly update the progressbar. Any help is appreciated. Thanks

tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,781
#2: May 16 '09

re: Problem related to progressbar control


Quote:
My problem seems to be that since i cant determine exactly how many records
If you don't know how many records, then how are you able to go through them all? You either know how many at *some* point or you don't. If you don't know how many you are working with then not only could you not accurately produce a progress bar, but you wouldn't be able to ensure you are handling them all.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#3: May 20 '09

re: Problem related to progressbar control


When you read from the Database you will know the number of records that you are manipulating.

Set the progress bar's Maximum property to this number.....

VB code example:
Expand|Select|Wrap|Line Numbers
  1. Dim totalNumberOfRecords As Integer 'This number is set when you retrieve the records from the database
  2.  
  3. Private Sub CopyWithProgress(ByVal ParamArray filenames As String())
  4.     ' Display the ProgressBar control.
  5.     pBar1.Visible = True
  6.     ' Set Minimum to 1 to represent the first file being copied.
  7.     pBar1.Minimum = 1
  8.     ' Set Maximum to the total number of records
  9.     pBar1.Maximum = totalNumberOfRecords
  10.     ' Set the initial value of the ProgressBar.
  11.     pBar1.Value = 1
  12.     ' Set the Step property to a value of 1 to represent each file being copied.
  13.     pBar1.Step = 1
  14.  
  15.     ' Loop through all files to copy.
  16.     Dim x As Integer
  17.     for x = 1 To totalNumberOfRecords - 1
  18.           'Do Stuff....Then
  19.           'Perform the increment on the ProgressBar.
  20.            pBar1.PerformStep()
  21.     Next x
  22. End Sub

C# code:
Expand|Select|Wrap|Line Numbers
  1. Integer  totalNumberOfRecords // This number is set when you retrieve the records from the database
  2. private void CopyWithProgress(string[] filenames)
  3. {
  4.   // Display the ProgressBar control.
  5.   pBar1.Visible = true;
  6.   // Set Minimum to 1 to represent the first file being copied.
  7.   pBar1.Minimum = 1;
  8.   // Set Maximum to the total number records.
  9.   pBar1.Maximum =totalNumberOfRecords ;
  10.   // Set the initial value of the ProgressBar.
  11.   pBar1.Value = 1;
  12.   // Set the Step property to a value of 1 to represent each file being copied.
  13.   pBar1.Step = 1;
  14.  
  15.    // Loop through all files to copy.
  16.    for (int x = 1; x <= totalNumberOfRecords; x++)
  17.    {  //Do Stuff...Then:
  18.       // Perform the increment on the ProgressBar.
  19.      pBar1.PerformStep();
  20.  
  21.    }
  22. }
Reply