What is a Desktop Clock? A desktop clock is a clock that runs
on your desktop. This is the first desktop application that I built when
learning C#. Here you’d learn how to build your own desktop clock in
5 minutes using a new technique.
Locate Form1 constructor and below the initialize components write this single line of code
timer1.Start();
At this point your codes should look like this
You are going to use a method called the K! Construct. K! Construct states that “timers can
substitute threads”.
First you must have a C# platform to run the program e.g
Visual Studio .
.
Create a new Project
- File -> New -> Project
- Select a Windows Form Application
- Then type in the name of application followed by (.cs)
- e.g myDesktopClock.cs
- Select Ok
..
Design the Interface
- An Empty windows form opens just like this.
- Add a label to the form
- Select the label and to adjust properties
- Set Auto Size – False
- Set Text – Time
- Set Text Align - Middle
- Resize the label and form to your desired size
- Locate the timer tool on your Tool box to drag and drop the tool on the form.
- Note that the tool does not appear on the form but would appear below the form as timer1.
- Double click timer1 and it automatically creates an event handler that handles the default event of the timer which is Tick and opens Form1.cs for coding.
...
Write these Codes (3 lines)
Inside the timer1.Tick event handler / method input these codes
DateTime time = DateTime.Now;
label1.Text = time.ToLongString ();
Locate Form1 constructor and below the initialize components write this single line of code
timer1.Start();
At this point your codes should look like this
Your Desktop Clock should be running now and look like this
By adding an image, color e.t.c you can have this
How it works
The timer by default ticks at an interval of 100 milliseconds,
and it starts ticking after all other components are initialized. At every tick, the text in label1 is
replaced with the current time on your system. By default the timer tick interval is 100 i.e. (100 millisecond == 1 sec).
The first time I programmed a desktop clock was in a csc322
lecture. Our lecturer wrote long codes i.e 3 classes (> 50 lines of codes). The truth
is that we spent time coding unnecessary lines, and the concept used was
threading i.e we used threads in our code.
While programming K! Music Player, it needed threads and the
worst thing's that threads sometimes misbehave. I experienced it in Kezer (a
browser I developed) on execution several times. So I tried different loop
structures to eliminate this problem and so on until I saw the timer tool and
read its function. Frankly speaking I think timers can replace threads and that’s
what makes K! Music Player unique (it runs on timers not threads).
With this discovery, you
now have a Desktop Clock with a new construct
>> the K! Construct…..