Monday, August 3, 2009

Progress Bar demo

Hi All,

I am Developing this Application just to help a newbie in the technology and even many of my students have asked me for the application which will demonstrate them how to use a progress bar in C#.net for a windows application. So here is a small application which will solve the purpose.

So Here Is the application and description how to develop the application.

1. Create a New Project in VS 2008 or above as per your convince I have installed VS 2008 so I am using that if you want to use VS 2005 you can even go with that also. For now VS2008


2. Create New Project : Select Windows Forms Application under the Visual C# tab and give the name to the project as prjProgressBar.

3. Now Place The ProgressBar Control and the Button Tool on the form from the tools box as shown in the figure below.

4. Set the following Properties of the progress bar:

  • Minimum =0 (By Default). This value will specify the lower bound for the progress bar.
  • Maximum =500 (Default value is 100). This is the upper bound value of the progress bar when progress is complete.
  • Step= 5 (Default is 10) This value is the value by which the progressbar will increase the current value with the next step when performstep function will be called. (in this case 500/5=100 i.e. in ten steps we will complete the progressbar.)
  • Value =0 (Default is 0) This will set or get the current value of the progressbar.
  • Style is the cosmetic property of the control default is blocks you may change it to marquee or continues. I am not changing it.
  • MarqueeanimationSpeed=100 (default is also 100): this is the speed in milisseconds set for the animation of the progressbar.

Now On the button click event we will start the progress bar and show a message at final step that in how many steps we have completed the progress bar. The code will be:

private void button1_Click(object sender, EventArgs e)
{
int i = 0;
while (progressBar1.Value < progressBar1.Maximum )
{
MessageBox.Show("Step : " + i);
}
}

>Now The above code I have written on the button click so you can write it at any place you needed. Suppose You want that as the data is begin added in the database then you need to perform a step in the database you can do that. But in that case you should set the maximum value of the progressbar to the total no. of records to be inserted and this will be set by the code at the event where you have calculated the total no. of records. with the help of this line.

progressBar1.Maximum=NoRecs;

and the step value should be 1 and when the insertion is completed in the database then use
progressBar1.PerformStep();
line of code there.

The Sample Code is attached.


Thanks and Regards
Meetu Choudhary
MVP || My Blog || MySite || My Forums

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner

MSDotnetMentor

MSDotnetMentor My Website http://msdotnetmentor.com