Adding Timer Control to WindowsForm

I had created one Windows Application in C# which include Timer intrevel and ran on it’s own thread.

This application consist of 1 textbox (ID=textBoxTime), 1 button (ID=buttonSubmitTime). When you enter a number value into the textbox that value gets converted to an int. This application assumes any value entered in the textbox is the number of minutes you wish the timer to be set for.

When you click the submit button this will create the timer and set it to the number of minutes desired. After the timer as started and gone through its interval it will display a message on screen. This should help you get started.

  1. Create Windows Form Application.
  2. Add 1 Textbox (ID=textBoxTime) and 1 Button (ID=buttonSubmitTime) to the form.
  3. Copy the given code to the cs file.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace WindowsFormsApplication1

{

public partial class Form1 : Form

{

private Timer myTimer;

private int holdTime = 0;

public Form1()

{

InitializeComponent();

// EVENT CALLERS

this.EventCallers();

}

private void EventCallers()

{

// SUBMIT TIME BUTTON – SUBMITS THE VALUE ENTERED IN THE TEXTBOX

this.buttonSubmitTime.Click += new EventHandler(buttonSubmitTime_Click);

}

// EVENT HANDLERS

#region EVENT HANDLERS

private void buttonSubmitTime_Click(object sender, EventArgs e)

{

// GET OUR VALUE FROM THE TEXTBOX

this.holdTime = int.Parse(this.textBoxTime.Text.ToString());

// CALL OUR START TIMER METHOD

this.StartTimer();

}

private void myTimer_Tick(object sender, EventArgs e)

{

// STOP THE TIMER

this.myTimer.Stop();

// SET TIMER ENABLED STATUS

this.myTimer.Enabled = false;

// SHOW A MESSAGE ON SCREEN SHOWING THE TIMER INTERVAL HAS PASSED

MessageBox.Show(“Alarm – Timer interval has passed”);

}

#endregion

private void StartTimer()

{

// CREATE A NEW INSTANCE OF OUR TIMER

this.myTimer = new Timer();

// SET OUR TIMERS INTERVAL TO THE VALUE FROM THE TEXTBOX

// IT IS ASSUMED ANY VALUE IN THE TEXTBOX IS A MINUTE

// FOR THE TIMER INTERVAL 1000 = 1 SECOND

// 60000 = 1 MIN

// IF YOU ENTERED 30 INTO THE TEXTBOX THE INTERVAL WOULD BE 1,800,000

this.myTimer.Interval = this.holdTime * 1000;

// START THE TIMER

this.myTimer.Start();

// SET TIMER TO ENABLED

this.myTimer.Enabled = true;

// OUR EVENT CALLER FOR WHEN THE TIMER INTERVAL IS COMPLETE

this.myTimer.Tick += new EventHandler(myTimer_Tick);

}

}

}OutputWindow

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Timer myTimer;
private int holdTime = 0;
public Form1()
{
InitializeComponent();
// EVENT CALLERS
this.EventCallers();
}
private void EventCallers()
{
// SUBMIT TIME BUTTON – SUBMITS THE VALUE ENTERED IN THE TEXTBOX
this.buttonSubmitTime.Click += new EventHandler(buttonSubmitTime_Click);
}
// EVENT HANDLERS
#region EVENT HANDLERS
private void buttonSubmitTime_Click(object sender, EventArgs e)
{
// GET OUR VALUE FROM THE TEXTBOX
this.holdTime = int.Parse(this.textBoxTime.Text.ToString());
// CALL OUR START TIMER METHOD
this.StartTimer();
}
private void myTimer_Tick(object sender, EventArgs e)
{
// STOP THE TIMER
this.myTimer.Stop();
// SET TIMER ENABLED STATUS
this.myTimer.Enabled = false;
// SHOW A MESSAGE ON SCREEN SHOWING THE TIMER INTERVAL HAS PASSED
MessageBox.Show(“Alarm – Timer interval has passed”);
}
#endregion
private void StartTimer()
{
// CREATE A NEW INSTANCE OF OUR TIMER
this.myTimer = new Timer();
// SET OUR TIMERS INTERVAL TO THE VALUE FROM THE TEXTBOX
// IT IS ASSUMED ANY VALUE IN THE TEXTBOX IS A MINUTE
// FOR THE TIMER INTERVAL 1000 = 1 SECOND
// 60000 = 1 MIN
// IF YOU ENTERED 30 INTO THE TEXTBOX THE INTERVAL WOULD BE 1,800,000
this.myTimer.Interval = this.holdTime * 1000;
// START THE TIMER
this.myTimer.Start();
// SET TIMER TO ENABLED
this.myTimer.Enabled = true;
// OUR EVENT CALLER FOR WHEN THE TIMER INTERVAL IS COMPLETE
this.myTimer.Tick += new EventHandler(myTimer_Tick);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Advertisements
Posted in CSharp. Tags: . Leave a Comment »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: