Introduction
This is a small Windows app which displays the current time in a binary way. It is built using Visual Studio 2008 Beta 2 and C#. I didn't aim to use any features specific to the .NET Framework 3.5 or VS 2008; however, involuntarily, I did use them - mostly having to do with the interface and debugging.
Background
While working on a project at work, I stumbled upon the JavaScript Tix clock. I found it neat and researched more. Then I saw the Binary clock. I decided to create this small binary clock of my own to serve the following purposes:
- Try Visual Studio 2008 while in a relaxed mode;
- Practice other fields of programming (I mostly work with ASP.NET and databases);
- Refresh C# in my head, for lately, most of my work is done in VB and JavaScript;
- Write a decimal to binary function which would leverage .NET;
- Have my very own binary clock "ticking" in the corner of the screen.
Using the Code
You can download the whole project, or just use the following code. The application consists of a simple Windows Form, which has only PictureBoxes on it named Hr10, Hr1 etc. The following is a code excerpt with the functions which make the clock (the rest is decor and the base requirements).
public partial class BinaryClock : Form
{
private System.Drawing.Color colLit;
private System.Drawing.Color colDim;
private System.Drawing.Color colBackground;
private System.Windows.Forms.PictureBox[] Hrs10;
private System.Windows.Forms.PictureBox[] Hrs;
private System.Windows.Forms.PictureBox[] Mns10;
private System.Windows.Forms.PictureBox[] Mns;
private System.Windows.Forms.PictureBox[] Scs10;
private System.Windows.Forms.PictureBox[] Scs;
public BinaryClock()
{
InitializeComponent();
}
private void BinaryClock_Load(object sender, EventArgs e)
{
this.ColorPattern(this.mnuBlue.Name); this.TransparencyKey = this.colBackground;
Hrs10 = new System.Windows.Forms.PictureBox[] { hr20, hr10 };
Hrs = new System.Windows.Forms.PictureBox[] { hr8, hr4, hr2, hr1 };
Mns10 = new System.Windows.Forms.PictureBox[] { mn40, mn20, mn10 };
Mns = new System.Windows.Forms.PictureBox[] { mn8, mn4, mn2, mn1 };
Scs10 = new System.Windows.Forms.PictureBox[] { sc40, sc20, sc10 };
Scs = new System.Windows.Forms.PictureBox[] { sc8, sc4, sc2, sc1 };
timBinClock_Tick(sender, e);
timBinClock.Start(); }
private void timBinClock_Tick(object sender, EventArgs e)
{
int Foo;
int HrNow = DateTime.Now.Hour; int TenHrNow = Math.DivRem(DateTime.Now.Hour, 10, out Foo) * 10;
bool[] hrs = this.DecimalToBinary(HrNow - TenHrNow, 8);
bool[] hrs10 = this.DecimalToBinary(TenHrNow / 10, 8);
LightOnOff(hrs10, this.Hrs10); LightOnOff(hrs, this.Hrs);
int MinNow = DateTime.Now.Minute;
int TenMinNow = Math.DivRem(DateTime.Now.Minute, 10, out Foo) * 10;
bool[] mns = this.DecimalToBinary(MinNow - TenMinNow, 8);
bool[] mns10 = this.DecimalToBinary(TenMinNow / 10, 8);
LightOnOff(mns10, this.Mns10);
LightOnOff(mns, this.Mns);
int SecNow = DateTime.Now.Second;
int TenSecNow = Math.DivRem(DateTime.Now.Second, 10, out Foo) * 10;
bool[] scs = this.DecimalToBinary(SecNow - TenSecNow, 8);
bool[] scs10 = this.DecimalToBinary(TenSecNow / 10, 8);
LightOnOff(scs10, this.Scs10);
LightOnOff(scs, this.Scs);
}
private bool[] DecimalToBinary(int number, int MaxBit)
{
System.Collections.ArrayList alBinaryNumber =
new System.Collections.ArrayList();
while (MaxBit >= 1)
{
if (number / MaxBit > 0)
{
alBinaryNumber.Add(true);
number = number - MaxBit;
}
else {
alBinaryNumber.Add(false); }
MaxBit = MaxBit / 2;
}
return (bool[])alBinaryNumber.ToArray(typeof(bool));
}
private void LightOnOff(bool[] binNumber, System.Windows.Forms.PictureBox[] Lights)
{
for (int i = (binNumber.Length - Lights.Length);
i - (binNumber.Length - Lights.Length) < Lights.Length; i++)
{
if (binNumber[i]) {
Lights.ElementAt(i -
(binNumber.Length - Lights.Length)).BackColor = this.colLit;
}
else
{
Lights.ElementAt(i -
(binNumber.Length - Lights.Length)).BackColor = this.colDim;
}
Lights.ElementAt(i - (binNumber.Length - Lights.Length)).Refresh();
}
}
Decimal to Binary
In the project itself, we use decimal to binary conversion, while specifying the maximum bit number. That is required for we need a fixed length byte. Below is a "pure" decimal to binary conversion where the maximum number of bits is calculated on the spot.
private bool[] DecimalToBinary(int number)
{
int MaxBit = 1;
while (MaxBit <= (number / 2))
{
MaxBit = MaxBit * 2;
}
System.Collections.ArrayList alBinaryNumber =
new System.Collections.ArrayList();
while (MaxBit >= 1)
{
if (number / MaxBit > 0)
{
alBinaryNumber.Add(true);
number = number - MaxBit;
}
else {
alBinaryNumber.Add(false); }
MaxBit = MaxBit / 2;
}
return (bool[])alBinaryNumber.ToArray(typeof(bool));
}
Points of Interest
- Uses Framework 3.5 and VS2008 - not particularly leveraging them, but still interesting. The Studio seems fine - actually enjoyed the interface and debugging. Despite the fact that it was Beta 2, I didn't encounter any major problems. There was a minor one: at a certain instance, the Solution Explorer stopped reacting on me pressing buttons which switch the view to form, to code etc... it's minor. Otherwise, I really liked the new Studio - it's the way to go.
- Implemented control array, i.e., created an array of controls which is much more flexible than in VB 6.
- Decimal to binary function uses an
ArrayList which gets converted to an array of booleans - no rocket science, but neat and efficient. Some people use strings... while for this app it's not important, it is a good habit to avoid strings in such circumstances.