dcsimg
 

Creating a Digital Clock in .NET

Thursday Jun 21st 2018 by Hannes DuPreez

Discover how quick and easy it is to create a digital clock in VB.NET and in C#.

Time: For some, there is enough time. For others, like me, there will never be enough time to accomplish everything and to do everything I'd like to do. I can do with a 30 hour day. Let me explain an ordinary day in the life of Hannes: I wake up at 4 AM. In the winters, this proves quite difficult! Wife and daughter go to the gym at 4:30 AM. I do some chores an around the house. At 6 AM, I start my commute to work, which is 60 KM (37 miles) away. Now, this doesn't sound too bad—until you hit Johannesburg traffic!

Like Forest Gump used to say: "Life is like a box of chocolates, because you never know what you're going to get." This is Johannesburg traffic. Some mornings, the commute can take you an hour (I do not have a flashy car, because I don't need a flashy car—I need a car that can take me from point A to point B—I do not need to show off anything), or the commute can take you 3 to 4 hours. Seriously.

I start working at 8, but I prefer being at the office at 7. This gives me enough time to have a second breakfast (I have a very fast metabolism...), coffee and a smoke, and time to get my thoughts for the day in order. I work until 5 PM. Then, the commute back. The afternoons are not too bad, unless there are major issues like a flood, or highway speed chase, or protest actions.

Then, I am home, watch a bit of TV and fall asleep in front of the TV. I suppose it is due to the fact that I am getting older... When I do not fall asleep early, I write articles such as this one. Weekends are too short. When I do not have to go to my chiropractor, I help out at a local college. Once that is done, I am home and can finally spend time with my family.

Every now and then, I go camping or exploring my beautiful country on weekends, but then time is way too short! So, as you can see, having 30 hour days can help me get some sleep. I average about four hours of sleep daily.

All of this got me thinking of an idea for an article, this one. Today, I will show you how quick and easy it is to create a digital clock in VB.NET and in C#. A few years ago, I wrote an article on how to create an analog clock; feel free to read it here.

Our Project

Open Visual Studio and create a Windows Forms project. Once the form has loaded, design it as shown in Figure 1. Make sure to include 8 Pictureboxes (these will hold the digits), an ImageList, and a Timer.

Design
Figure 1: Design

I cannot draw. At all. You will see proof of this fact a bit later. Create images for the digits, from 0 to 9, and include a colon. These images will be used to display the time, so if you are blessed enough to be able to draw, draw nice digits, or download a font that looks funky and use it. Once you have your pictures, add them all to the ImageList.

ImageList Images
Figure 2: ImageList Images

Make sure your Timer's Enabled Property is set to True in Design Time.

Code

Let's get cracking!

Add the following Function that will return the appropriate image to the timer.

C#

      private Image Index(string strNum)
      {
         return ImageList2.Images[Convert.ToInt32(strNum)];
      }

VB.NET

   Private Function Index(ByVal strNum As String) As Image
      Return ImageList2.Images(Convert.ToInt32(strNum))
   End Function

Add the Form_Load event.

C#

      private void Form1_Load(object sender, EventArgs e)
      {
         Timer1.Interval = 1;
         Timer1.Enabled = true;
         PictureBox3.Image = ImageList2.Images[10];
         PictureBox6.Image = ImageList2.Images[10];
      }

VB.NET

   Private Sub Form1_Load(ByVal sender As Object, _
         ByVal e As EventArgs)
      Timer1.Interval = 1
      Timer1.Enabled = True
      PictureBox3.Image = ImageList2.Images(10)
      PictureBox6.Image = ImageList2.Images(10)
   End Sub

The Form_Load event initializes the Timer and sets PictureBox3's Image to the Colon. This will act as a separator for hours, minutes, and seconds.

Add the Timer_Tick event.

C#

      private void Timer1_Tick(object sender, EventArgs e)
      {
         string strDateTime = DateTime.Now.ToString();
         int strDelimiter = strDateTime.IndexOf(" ");
         string strTime = strDateTime.Substring(strDelimiter + 1,
            strDateTime.Length - strDelimiter - 1);
         PictureBox1.Image = Index(strTime[0].ToString());
         PictureBox2.Image = Index(strTime[1].ToString());
         PictureBox4.Image = Index(strTime[3].ToString());
         PictureBox5.Image = Index(strTime[4].ToString());
         PictureBox7.Image = Index(strTime[6].ToString());
         PictureBox8.Image = Index(strTime[7].ToString());
      }

VB.NET

   Private Sub Timer1_Tick(sender As Object, e As EventArgs) _
         Handles Timer1.Tick
      Dim strDateTime As String = DateTime.Now.ToString()
      Dim strDelimiter As Integer = strDateTime.IndexOf(" ")
      Dim strTime As String = strDateTime.Substring(strDelimiter _
         + 1, strDateTime.Length - strDelimiter - 1)
      PictureBox1.Image = Index(strTime(0).ToString())
      PictureBox2.Image = Index(strTime(1).ToString())
      PictureBox4.Image = Index(strTime(3).ToString())
      PictureBox5.Image = Index(strTime(4).ToString())
      PictureBox7.Image = Index(strTime(6).ToString())
      PictureBox8.Image = Index(strTime(7).ToString())
   End Sub

When run, your project will look similar to Figure 3.

Running
Figure 3: Running

Conclusion

A famous quote goes: "Time Is What Prevents Everything From Happening At Once." I hope you have enjoyed today's article. Until next time, happy coding!

Home
Mobile Site | Full Site