Windows Controls: The Timer |
|
Description
A timer is a non-spatial object that uses recurring lapses of time in a computer or in your application. To work, every lapse of period, the control sends a message to the operating system. The message is something to the effect of "I have counted the number of lapses you asked me to count".
As opposed to the time that controls your computer, a timer is partly but greatly under your control. Users do not see nor do they use a timer as a control. As a programmer, you decide if, why, when, and how to use this control.
To support timers, the .NET Framework provides the Timer control from the Toolbox and it is implemented through the Timer class. To add it to your application at design time, on the Toolbox, click Timer and click the form.
Practical Learning: Introducing the Timer Control
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 ScreenSaver1 { public partial class Form1 : Form { static int MoveCounter = 0; public Form1() { InitializeComponent(); } private void Form1_MouseMove(object sender, MouseEventArgs e) { Cursor.Hide(); if (MoveCounter == 20) Close(); MoveCounter++; } } }
private void Form1_KeyDown(object sender, KeyEventArgs e) { // If the user presses Esc, stop the screen saver if (e.KeyCode == Keys.Escape) Close(); }
Characteristics of a Timer
A timer is an object used to count lapses of time and send a message when it has finished counting. Each count is called a tick. When a tick occurs, the control fires a Tick event. This Tick event is of type EventArgs, meaning that it doesn't provide more information than to let you know that a lapse has occurred. Here is an example of implementing that event:
using System;
using System.Drawing;
using System.Windows.Forms;
public class Exercise : Form
{
Timer tmrFlash;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
tmrFlash = new Timer ();
tmrFlash.Tick += new EventHandler(btnFlashTick);
Text = "Text Flasher";
Size = new System.Drawing.Size(435, 88);
}
void btnFlashTick(object sender, EventArgs e)
{
}
}
public class Program
{
static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}
The amount of time allocated for counting is called an interval and it is represented by the Interval property. The Interval is a very important characteristic of the Timer control because it measures and controls the total time needed to perform a complete count. The Interval is measured in milliseconds. Like any counter, the lower the value, the faster the count, and the higher the value, the longer the count (if you ask one kid to count from 1 to 10 and you ask another to count from 1 to 20 at the same time, if you ask them to shout when they finish, the first kid would finish first and would shout first). The amount of interval you specify will depend on what you are trying to do. Here is an example of specifying the interval:
void InitializeComponent()
{
tmrFlash = new Timer ();
tmrFlash.Interval = 500;
tmrFlash.Tick += new EventHandler(btnFlashTick);
}
In order for a timer to count, you must tell it when it should start counting. In some applications, you may want the control to work full-time while in some other applications, you may want the control to work only in response to an intermediate event. The ability to stop and start a Timer control can be set using the Enabled Boolean property. Here is an example of enabling a timer:
using System;
using System.Drawing;
using System.Windows.Forms;
public class Exercise : Form
{
Timer tmrFlash;
Label lblFlash;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
tmrFlash = new Timer();
tmrFlash.Interval = 500;
tmrFlash.Enabled = true;
tmrFlash.Tick += new EventHandler(btnFlashTick);
lblFlash = new Label();
lblFlash.AutoSize = true;
lblFlash.Text = "C# Programming is Fun!!!";
lblFlash.Font = new System.Drawing.Font("Square721 BT", 24F);
lblFlash.ForeColor = System.Drawing.Color.Blue;
lblFlash.Location = new System.Drawing.Point(12, 10);
Text = "Text Flasher";
Size = new System.Drawing.Size(435, 88);
Controls.Add(lblFlash);
}
void btnFlashTick(object sender, EventArgs e)
{
if (lblFlash.Text == "C# Programming is Fun!!!")
lblFlash.Text = "";
else
lblFlash.Text = "C# Programming is Fun!!!";
}
}
public class Program
{
static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}
When, or as soon as, this property is set to true, the control starts counting. You can also make it start by calling the Timer.Start() method. Its syntax is:
public void Start();
If, when, or as soon as, the Enabled property is set to false, the control stops and resets its counter to 0. You can also stop the timer by calling the Timer.Stop() method. Its syntax is:
public void Stop();
Practical Learning: Using Timer Controls
private void timer1_Tick(object sender, EventArgs e) { // Get the Graphics object of the form Graphics graph = Graphics.FromHwnd(this.Handle); // Generate a random number Random rndNumber = new Random(DateTime.Now.Millisecond); // Create a list of the colors of the .NET Framework Color[] curColor = { Color.AliceBlue, Color.AntiqueWhite, Color.Aqua, Color.Aquamarine, Color.Azure, Color.Beige, Color.Bisque, Color.Black, Color.BlanchedAlmond, Color.Blue, Color.BlueViolet, Color.Brown, Color.BurlyWood, Color.CadetBlue, Color.Chartreuse, Color.Chocolate, Color.Coral, Color.CornflowerBlue, Color.Cornsilk, Color.Crimson, Color.Cyan, Color.DarkBlue, Color.DarkCyan, Color.DarkGoldenrod, Color.DarkGray, Color.DarkGreen, Color.DarkKhaki, Color.DarkMagenta, Color.DarkOliveGreen, Color.DarkOrange, Color.DarkOrchid, Color.DarkRed, Color.DarkSalmon, Color.DarkSeaGreen, Color.DarkSlateBlue, Color.DarkSlateGray, Color.DarkTurquoise, Color.DarkViolet, Color.DeepPink, Color.DeepSkyBlue, Color.DimGray, Color.DodgerBlue, Color.Firebrick, Color.FloralWhite, Color.ForestGreen, Color.Fuchsia, Color.Gainsboro, Color.GhostWhite, Color.Gold, Color.Goldenrod, Color.Gray, Color.Green, Color.GreenYellow, Color.Honeydew, Color.HotPink, Color.IndianRed, Color.Indigo, Color.Ivory, Color.Khaki, Color.Lavender, Color.LavenderBlush, Color.LawnGreen, Color.LemonChiffon, Color.LightBlue, Color.LightCoral, Color.LightCyan, Color.LightGoldenrodYellow, Color.LightGray, Color.LightGreen, Color.LightPink, Color.LightSalmon, Color.LightSeaGreen, Color.LightSkyBlue, Color.LightSlateGray, Color.LightSteelBlue, Color.LightYellow, Color.Lime, Color.LimeGreen, Color.Linen, Color.Magenta, Color.Maroon, Color.MediumAquamarine, Color.MediumBlue, Color.MediumOrchid, Color.MediumPurple, Color.MediumSeaGreen, Color.MediumSlateBlue, Color.MediumSpringGreen, Color.MediumTurquoise, Color.MediumVioletRed, Color.MidnightBlue, Color.MintCream, Color.MistyRose, Color.Moccasin, Color.NavajoWhite, Color.Navy, Color.OldLace, Color.Olive, Color.OliveDrab, Color.Orange, Color.OrangeRed, Color.Orchid, Color.PaleGoldenrod, Color.PaleGreen, Color.PaleTurquoise, Color.PaleVioletRed, Color.PapayaWhip, Color.PeachPuff, Color.Peru, Color.Pink, Color.Plum, Color.PowderBlue, Color.Purple, Color.Red, Color.RosyBrown, Color.RoyalBlue, Color.SaddleBrown, Color.Salmon, Color.SandyBrown, Color.SeaGreen, Color.SeaShell, Color.Sienna, Color.Silver, Color.SkyBlue, Color.SlateBlue, Color.SlateGray, Color.Snow, Color.SpringGreen, Color.SteelBlue, Color.Tan, Color.Teal, Color.Thistle, Color.Tomato, Color.Transparent, Color.Turquoise, Color.Violet, Color.Wheat, Color.White, Color.WhiteSmoke, Color.Yellow, Color.YellowGreen }; // Create a list of 10 rectangles for each row Rectangle[] row1 = new Rectangle[10]; Rectangle[] row2 = new Rectangle[10]; Rectangle[] row3 = new Rectangle[10]; Rectangle[] row4 = new Rectangle[10]; Rectangle[] row5 = new Rectangle[10]; Rectangle[] row6 = new Rectangle[10]; Rectangle[] row7 = new Rectangle[10]; Rectangle[] row8 = new Rectangle[10]; // Create the rectangles that will be drawn on the screen for (int i = 0; i < 9; i++) { row1[i] = new Rectangle(i + (i * (Width / 10)), 0, (Width - 36) / 10, Height / 8); row2[i] = new Rectangle(i + (i * (Width / 10)), 4 + (Height / 8), (Width - 36) / 10, Height / 8); row3[i] = new Rectangle(i + (i * (Width / 10)), 8 + (2 * (Height / 8)), (Width - 36) / 10, Height / 8); row4[i] = new Rectangle(i + (i * (Width / 10)), 12 + (3 * (Height / 8)), (Width - 36) / 10, Height / 8); row5[i] = new Rectangle(i + (i * (Width / 10)), 16 + (4 * (Height / 8)), (Width - 36) / 10, Height / 8); row6[i] = new Rectangle(i + (i * (Width / 10)), 20 + (5 * (Height / 8)), (Width - 36) / 10, Height / 8); row7[i] = new Rectangle(i + (i * (Width / 10)), 24 + (6 * (Height / 8)), (Width - 36) / 10, Height / 8); row8[i] = new Rectangle(i + (i * (Width / 10)), 28 + (7 * (Height / 8)), (Width - 36) / 10, Height / 8); } // Create the last rectangle of each row Rectangle row1a = new Rectangle(9 + (9 * (Width / 10)), 0, ((Width - 36) / 10) - 2, Height / 8); Rectangle row2a = new Rectangle(9 + (9 * (Width / 10)), 4 + (Height / 8), ((Width - 36) / 10) - 2, Height / 8); Rectangle row3a = new Rectangle(9 + (9 * (Width / 10)), 8 + (2 * (Height / 8)), ((Width - 36) / 10) - 2, Height / 8); Rectangle row4a = new Rectangle(9 + (9 * (Width / 10)), 12 + (3 * (Height / 8)), ((Width - 36) / 10) - 2, Height / 8); Rectangle row5a = new Rectangle(9 + (9 * (Width / 10)), 16 + (4 * (Height / 8)), ((Width - 36) / 10) - 2, Height / 8); Rectangle row6a = new Rectangle(9 + (9 * (Width / 10)), 20 + (5 * (Height / 8)), ((Width - 36) / 10) - 2, Height / 8); Rectangle row7a = new Rectangle(9 + (9 * (Width / 10)), 24 + (6 * (Height / 8)), ((Width - 36) / 10) - 2, Height / 8); Rectangle row8a = new Rectangle(9 + (9 * (Width / 10)), 28 + (7 * (Height / 8)), ((Width - 36) / 10) - 2, Height / 8); // Create a list of the hatch brushes of the .NET Framework using random colors HatchBrush[] curBrush = { new HatchBrush(HatchStyle.BackwardDiagonal, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.Cross, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.DarkDownwardDiagonal, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.DarkHorizontal, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.DarkUpwardDiagonal, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.DarkVertical, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.DashedDownwardDiagonal, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.DashedHorizontal, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.DashedUpwardDiagonal, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.DashedVertical, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.DashedVertical, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.DiagonalBrick, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.DiagonalCross, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.Divot, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.DottedDiamond, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.DottedGrid, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.ForwardDiagonal, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.Horizontal, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.HorizontalBrick, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.LargeCheckerBoard, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.LargeConfetti, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.LargeGrid, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.LightDownwardDiagonal, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.LightHorizontal, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.LightUpwardDiagonal, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.LightVertical, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.Max, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.Min, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.NarrowHorizontal, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.NarrowVertical, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.OutlinedDiamond, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.Percent05, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.Percent10, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.Percent20, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.Percent25, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.Percent30, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.Percent40, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.Percent50, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.Percent60, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.Percent70, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.Percent75, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.Percent80, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.Percent90, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.Plaid, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]), new HatchBrush(HatchStyle.Percent05, curColor[rndNumber.Next(curColor.Length)], curColor[rndNumber.Next(curColor.Length)]) }; // Draw the rectangles to cover the screen for (int i = 0; i < 9; i++) { graph.FillRectangle(curBrush[rndNumber.Next(curBrush.Length)], row1[i]); graph.DrawRectangle(new Pen(curBrush[rndNumber.Next(curBrush.Length)]), row1[i]); graph.FillRectangle(curBrush[rndNumber.Next(curBrush.Length)], row2[i]); graph.DrawRectangle(new Pen(curBrush[rndNumber.Next(curBrush.Length)]), row2[i]); graph.FillRectangle(curBrush[rndNumber.Next(curBrush.Length)], row3[i]); graph.DrawRectangle(new Pen(curBrush[rndNumber.Next(curBrush.Length)]), row3[i]); graph.FillRectangle(curBrush[rndNumber.Next(curBrush.Length)], row4[i]); graph.DrawRectangle(new Pen(curBrush[rndNumber.Next(curBrush.Length)]), row4[i]); graph.FillRectangle(curBrush[rndNumber.Next(curBrush.Length)], row5[i]); graph.DrawRectangle(new Pen(curBrush[rndNumber.Next(curBrush.Length)]), row5[i]); graph.FillRectangle(curBrush[rndNumber.Next(curBrush.Length)], row6[i]); graph.DrawRectangle(new Pen(curBrush[rndNumber.Next(curBrush.Length)]), row6[i]); graph.FillRectangle(curBrush[rndNumber.Next(curBrush.Length)], row7[i]); graph.DrawRectangle(new Pen(curBrush[rndNumber.Next(curBrush.Length)]), row7[i]); graph.FillRectangle(curBrush[rndNumber.Next(curBrush.Length)], row8[i]); graph.DrawRectangle(new Pen(curBrush[rndNumber.Next(curBrush.Length)]), row8[i]); } graph.FillRectangle(curBrush[rndNumber.Next(curBrush.Length)], row1a); graph.DrawRectangle(new Pen(curBrush[rndNumber.Next(curBrush.Length)]), row1a); graph.FillRectangle(curBrush[rndNumber.Next(curBrush.Length)], row2a); graph.DrawRectangle(new Pen(curBrush[rndNumber.Next(curBrush.Length)]), row2a); graph.FillRectangle(curBrush[rndNumber.Next(curBrush.Length)], row3a); graph.DrawRectangle(new Pen(curBrush[rndNumber.Next(curBrush.Length)]), row3a); graph.FillRectangle(curBrush[rndNumber.Next(curBrush.Length)], row4a); graph.DrawRectangle(new Pen(curBrush[rndNumber.Next(curBrush.Length)]), row4a); graph.FillRectangle(curBrush[rndNumber.Next(curBrush.Length)], row5a); graph.DrawRectangle(new Pen(curBrush[rndNumber.Next(curBrush.Length)]), row5a); graph.FillRectangle(curBrush[rndNumber.Next(curBrush.Length)], row6a); graph.DrawRectangle(new Pen(curBrush[rndNumber.Next(curBrush.Length)]), row6a); graph.FillRectangle(curBrush[rndNumber.Next(curBrush.Length)], row7a); graph.DrawRectangle(new Pen(curBrush[rndNumber.Next(curBrush.Length)]), row7a); graph.FillRectangle(curBrush[rndNumber.Next(curBrush.Length)], row8a); graph.DrawRectangle(new Pen(curBrush[rndNumber.Next(curBrush.Length)]), row8a); }
|
||
Home | Copyright © 2010-2020, FunctionX | |
|