By Jie Su & Ying Qian
VS.Net has provided us with controls. In this paper, we demonstrate how to
use Timer, ProgressBar and Statusbar in an application by using C#.
Download Source Code - code197.zip
Timer Control
Create a timer control using the toolbox, the system will automatically add
the necessary codes.
this.Clock.Enabled = true;
this.Clock.Interval = 1000;
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();
}