Using ProgressBar, StatusBar and Timer Controls in VS.NET


This source code shows how to use the Timer, ProgressBar and StatusBar Windows Forms controls. The 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 by using C#.

Timer Control

First create a Windows 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.

this.Clock.Enabled = true;
this.Clock.Interval = 1000;

We also add the Tick event handler by double clicking on the timer control.

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.

public void Timer_Tick(object sender,EventArgs eArgs)
{
if(sender==Clock)
{
lbTime.Text=GetTime();
}
public string GetTime()
{
string TimeInString="";
int hour=DateTime.Now.Hour;
int min=DateTime.Now.Minute;
int sec=DateTime.Now.Second;
TimeInString=(hour < 10)?"0" + hour.ToString() :hour.ToString();
TimeInString+=":" + ((min<10)?"0" + min.ToString() :min.ToString());
TimeInString+=":" + ((sec<10)?"0" + sec.ToString() :sec.ToString());
return TimeInString;
}

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.

this.statusBar1.Location = new System.Drawing.Point(0, 273);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(416, 20);
this.statusBar1.TabIndex = 4;
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

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.

statusBar1.Text ="Database loaded";

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.

this.progressBar1.Location = new System.Drawing.Point(88, 232);
this.progressBar1.Name = "progressBar1";
this.progressBar1.TabIndex = 0;
this.progressBar1 .Maximum =10000;
this.progressBar1 .Minimum =1;
this.progressBar1 .Step =1;

The following codes will make the progressbar increase the step one per time.

for(int i=progressBar1.Minimum; i<=progressBar1.Maximum; i++)
{
progressBar1.PerformStep();
}

Up Next
    Ebook Download
    View all
    Learn
    View all