Get Microsoft visual studio 2012 here
Hello utopian, welcome to this tutorial. This happens to be my first tutorial and we will be looking at the concept of C# GUI, concept behind Timer control and creating a simple Windows Forms countdown timer.
Graphical User Interface is an interactive display which presents graphical elements/widgets for the user to interact with. It's made of graphical element that provides easy use, more visual interface for users to operate without having to know lots of commands such as a text box, a button, label, check box, Timer etc.
The Timer Control plays an important role in the development of programs both Client side and Server side development as well as in Windows Services. With the Timer Control we can raise events at a specific interval of time without the interaction of another thread.
A Timer control does not have a visual representation and works as a component in the background. We can control programs with Timer Control in millisecond, seconds, minute and even in hours. The Timer Control allows us to set Interval property in milliseconds. That is, one second is equal to 1000 milliseconds.
A countdown timer is program that takes user input into a masked textbox, uses the Systems.Windows.Forms.Timer class to count down while displaying the remaining time, and then beeps at you with a message box when the time is up.
C# provide user with a toolbox that support Drag and drop of control on the Form
That is, you will have three label, Three Button, One Textbox, One Panel and Timer control.
What I did first, I created a Message that displays the significance of the program.
then “this.Text = "Timer Application";” is to Rename the Title of the Form from the .Text Property.
While the “txtTimer.Text = "00:00";” is to set the Textbox default .text property to be "00:00" at runtime.
“paneltimer.Visible = false;” is to set the panel visibility to be false at runtime.
Private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show(" A countdown timer is program that takes user input into a masked textbox, \n"
+ "uses the Systems.Windows.Forms.Timer class to count down while displaying the remaining time, "
+ " and then beeps at you with a message box when the time is up.","WELCOME UTOPAIN");
this.Text = "Timer Application";
txtTimer.Text = "00:00";
paneltimer.Visible = false;
}
}
private void btnStart_Click(object sender, EventArgs e)
{
if (txtTimer.Text == "00:00")
{
MessageBox.Show("Please enter the time to start!", "Enter the Time", MessageBoxButtons.OK);
}
else
{
// Convert text to seconds as int for timer
string[] totalSeconds = txtTimer.Text.Split(':');
int minutes = Convert.ToInt32(totalSeconds[0]);
int seconds = Convert.ToInt32(totalSeconds[1]);
timeLeft = (minutes*60) + seconds;
// Lock Start and Clear buttons and text box
btnStart.Enabled = false;
btnReset.Enabled = false;
txtTimer.ReadOnly = true;
// start timer
timer1.Start();
}
}
private void btnReset_Click(object sender, EventArgs e)
{
txtTimer.Text = "00:00";
}
private void timer1_Tick(object sender, EventArgs e)
{
if (timeLeft > 0)
{
timeLeft = timeLeft - 1;
// Display time remaining as mm:ss
var timespan = TimeSpan.FromSeconds(timeLeft);
txtTimer.Text = timespan.ToString(@"mm\:ss");
// Alternate method
//int secondsLeft = timeLeft % 60;
//int minutesLeft = timeLeft / 60;
}
else
{
timer1.Stop();
SystemSounds.Exclamation.Play();
MessageBox.Show("Time's up!", "Time has elapsed", MessageBoxButtons.OK);
}
}
private void BtnStoP_Click(object sender, EventArgs e)
{
timer1.Stop();
timeLeft = 0;
btnStart.Enabled = true;
btnReset.Enabled = true;
txtTimer.ReadOnly = false;
}
private void lblproceed_Click(object sender, EventArgs e)
{
paneltimer.Visible = true;
lblproceed.Visible = false;
}
This is the first contribution in this curriculum. It will be succeed with more contributions.