Using Progress Bar

G

Guest

Dear, I have windows form , the form is using a class to do all tasks needed
like this
private void btnProcess_Click(object sender, System.EventArgs e)
{
btnProcess.Enabled = false;
MailBoxRenamer oMailBox = new MailBoxRenamer();
//How Can i make the progress bar moving while rename processing is
underway
pBarCtrl.Visible = true;
oMailBox.RenameAll();
MessageBox.Show("Operation Successfully Completed","Operation
Done",MessageBoxButtons.OK,MessageBoxIcon.Information);
btnProcess.Enabled = true;
}

oMailBox is object of MailBoxRenamer class that do following:

1. Get All Files in Specified Directory
2. Get the Name Of each folder and query the database about its relative ID
3. Rename the folder from the name to ID

All I need how to enable dynamic progress action during renaming
 
G

Guest

Hi raed,

I have a neat trick which can be used in this scenario. I am not sure
whether i am fully correct saying this because there may be other ways of
doing this. But this is how i have done.

I have created a Timer control with Interval Property set to 1000 and
Enalbled property set to TRUE.

The idea is to increment the progress bar in the timer control while your
component does the work of doint its task. Once the task is complete you can
disable the timer and then hide the progress bar. I have modifed your code
accordingly as below.


private void btnProcess_Click(object sender, System.EventArgs e)
{
btnProcess.Enabled = false;
MailBoxRenamer oMailBox = new MailBoxRenamer();
//How Can i make the progress bar moving while rename processing is
underway
pBarCtrl.Visible = true;

//enable timer
timer1.Enabled = true;

oMailBox.RenameAll();
MessageBox.Show("Operation Successfully Completed","Operation
Done",MessageBoxButtons.OK,MessageBoxIcon.Information);
btnProcess.Enabled = true;

//disable timer
timer1.Enabled = false;
//hide progress bar
pBarCtrl.Visible = true;

}


Now write the following int teh Tick event of the timer

private void timer1_Tick(object sender, System.EventArgs e)
{
//Incrementing the value by 10. You can also set to lower value to cause
more delay
progressBar1.Value += 10;
}


However you have to be careful about the interval that you set for the
timer. If the interval is not set properly then you might see that the
progress bar has reached its end before finishing its task. so set teh
interval accordingly.

Hope this helps.

Happy Programming!!
Pradeep_TP
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

The way to go is create a thread to do the work. You set the max value of
the progressbar to the qty of files you will convert and then after a
convertion you update the progressbar ( you have to use Control.Invoke here)

Hope this help,
 
C

CometJoe

I don't prefer the first answer, due to the creating of a fixed time
interval, when your folders will probably contain a different number
of files, and thus will take a different amount of time to convert.


One way to accomodate this is to expose a public function in your form
that updates the progress bar.
Set your progress bar Min = 0; and Maximum=100...


public void UpdateProgressBar( int nPercent )
{
progressBar1.Value = nPercent;
}

Then pass a handle to the main form down into the Rename function.
You'll need to change the rename function to accept the form handle.
You'll also need to calculate the value of the percent of completion
in the Rename function to use as it runs. Here is a cheesy
example...
[code:1:d5cb955934]
public void RenameMailboxes( frmMain oParentForm )
{
float fPercent = 0;
int nCounter = 0;
int nFiles = 128;

do
{
fPercent = ((float)nCounter /
(float)nFiles) * 100 ;//calculate completion
oParentForm.UpdateProgressBar( (int)fPercent );
//passes whole number
nCounter ++;
}while( fPercent < 100 );
}
[/code:1:d5cb955934]

The call into the Rename function passes in the handle to the calling
form, then the rename function is able to update the UI control as it
executes.

You'll need to change the call to the rename function to pass the
handle to the calling form...
[code:1:d5cb955934]
private void btnTest_Click(object sender, System.EventArgs
e)
{
Test oTest = new Test();
oTest.RenameMailboxes( this );
}

[/code:1:d5cb955934]

Hope This Helps,
Joe
 
G

Guest

ok where should I put my code to perform the task of renaming , so the
operation in progress bar keep in progress while my code execution
public void RenameMailboxes( frmMain oParentForm )
{
float fPercent = 0;
int nCounter = 0;
int nFiles = 128;

do
{
fPercent = ((float)nCounter /(float)nFiles) * 100 ;//calculate completion
oParentForm.UpdateProgressBar( (int)fPercent );
//passes whole number
nCounter ++;
}while( fPercent < 100 );
}
 

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