Using ProgressBar, StatusBar And Timer Controls In VS.NET

This source code shows how to use a Timer, a ProgressBar and a StatusBar contol in a WinForms app. The final application looks like following.
 
 
VS .NET provides many Windows and Web forms controls. In this article, we demonstrate how to use Timer, ProgressBar and Statusbar Windows Forms controls in an application using C#.

Timer Control
 
First create a Windows Forms application. We add a timer control by dragging and draping the control on the form from the toolbox, the system will automatically add the necessary code. After that we set timer control properties as Interval = 1000 (1 sec) and Enabled as true. Setting the Enabled property to true starts the timer control.
  1. this.Clock.Enabled = true;  
  2. this.Clock.Interval = 1000;  
We also add the Tick event handler by double clicking on the timer control.
  1. this.Clock.Tick += new System.EventHandler(this.Timer_Tick);  
When the form is loaded and the timer control starts ticking, the Timer_Tick method is invoked. The method will get the hours, minutes, seconds by calling the GetTime() method.
  1. public void Timer_Tick(object sender,EventArgs eArgs)  
  2.    {  
  3.       if(sender==Clock)  
  4.       {  
  5.          lbTime.Text=GetTime();  
  6.       }  
  7. public string GetTime()  
  8.    {  
  9.       string TimeInString="";  
  10.       int hour=DateTime.Now.Hour;  
  11.       int min=DateTime.Now.Minute;  
  12.       int sec=DateTime.Now.Second;  
  13.       TimeInString=(hour < 10)?"0" + hour.ToString() :hour.ToString();  
  14.       TimeInString+=":" + ((min<10)?"0" + min.ToString() :min.ToString());  
  15.       TimeInString+=":" + ((sec<10)?"0" + sec.ToString() :sec.ToString());  
  16.       return TimeInString;  
  17. }  
Here is a more detailed article on Timer in C#.
 
Status Bar
 
Use the toolbox to draw a status bar. It will go to the bottom of the window to show the current status of the operation. You may initialize the status bar by giving some appropriate words to the text property of the status bar.
  1. this.statusBar1.Location = new System.Drawing.Point(0, 273);  
  2. this.statusBar1.Name = "statusBar1";  
  3. this.statusBar1.Size = new System.Drawing.Size(416, 20);  
  4. this.statusBar1.TabIndex = 4;  
  5. this.statusBar1.Text = "Ready";  
Status bar is used to give users information about the current status. For example when some action is taken, it may be used to tell the user that the system is done with the operation. So the user will be aware of what has happened.
 
In this program we tried to load data from the remote server. It may take some time to grab the data from the server. The status bar is used to tell the user what the system is doing by setting the text property of the statusBar as follows
  1. statusBar1.Text="Connecting to DB...";  
When the system is done with the loading, we should tell the user that the data have been loaded. Then the user is aware that he or she can do something else.
  1. statusBar1.Text ="Database loaded";  
Note
StatusBar control has been replaced by a StatusStrip control. To learn more, read How to use a StatusStrip in C#.
 
Progress Bar
 
Progress bars are used to display the progress of your application or background tasks. There are three members of the ProgressBar class you should know about: the Maximum, the Minimum, and the Value properties.
 
After creating instance of a progress bar you set the range of the progress bar by using Minimum and Maximum properties of the ProgressBar.
 
The Step property is used to set number of steps in a progress bar. The Value property is used to set the current value of the progress bar.
  1. this.progressBar1.Location = new System.Drawing.Point(88, 232);  
  2. this.progressBar1.Name = "progressBar1";  
  3. this.progressBar1.TabIndex = 0;  
  4. this.progressBar1 .Maximum =10000;  
  5. this.progressBar1 .Minimum =1;  
  6. this.progressBar1 .Step =1;  
The following codes will make the progressbar increase the step one per time.
  1. for(int i=progressBar1.Minimum; i<=progressBar1.Maximum; i++)  
  2. {  
  3.    progressBar1.PerformStep();  
  4. }  
Here is a detailed article on Progress Bar in C#.
 
Summary
 
In this article, we discussed how to use a timer, status bar, and a progress bar control in Windows Forms.


Similar Articles