C# - Simple Digital Clock

In this tutorial we will create a Simple Digital Clock using C#. C# is a general-purpose, object-oriented programming language. C# automatically manages inaccessible object memory using a garbage collector, which eliminates developer concerns and memory leaks. It has a designed for improving productivity in the development of Web applications. It has a friendly environment for all new developers. So let's do the coding...

Getting Started

First you will have to download & install the Visual Studio. Visual Studios is an open source development feel free to create any application that you want. Here's the link for the Visual Studio https://www.visualstudio.com/.

Application Design

We will now create the design for the application, first locate the designer file called form1.Designer.cs, this is the default name when you create a new windows form. Rename the form as Main.cs and then write these codes inside your designer file.
  1. namespace Simple_Digital_Clock
  2. {
  3. partial class Main
  4. {
  5. /// <summary>
  6. /// Required designer variable.
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9.  
  10. /// <summary>
  11. /// Clean up any resources being used.
  12. /// </summary>
  13. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  14. protected override void Dispose(bool disposing)
  15. {
  16. if (disposing && (components != null))
  17. {
  18. components.Dispose();
  19. }
  20. base.Dispose(disposing);
  21. }
  22.  
  23. #region Windows Form Designer generated code
  24.  
  25. /// <summary>
  26. /// Required method for Designer support - do not modify
  27. /// the contents of this method with the code editor.
  28. /// </summary>
  29. private void InitializeComponent()
  30. {
  31. this.components = new System.ComponentModel.Container();
  32. this.lbl_time = new System.Windows.Forms.Label();
  33. this.lbl_sec = new System.Windows.Forms.Label();
  34. this.lbl_day = new System.Windows.Forms.Label();
  35. this.timer1 = new System.Windows.Forms.Timer(this.components);
  36. this.lbl_date = new System.Windows.Forms.Label();
  37. this.SuspendLayout();
  38. //
  39. // lbl_time
  40. //
  41. this.lbl_time.AutoSize = true;
  42. this.lbl_time.Font = new System.Drawing.Font("DS-Digital", 72F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  43. this.lbl_time.ForeColor = System.Drawing.Color.Green;
  44. this.lbl_time.Location = new System.Drawing.Point(88, 76);
  45. this.lbl_time.Name = "lbl_time";
  46. this.lbl_time.Size = new System.Drawing.Size(376, 142);
  47. this.lbl_time.TabIndex = 0;
  48. this.lbl_time.Text = "00:00";
  49. //
  50. // lbl_sec
  51. //
  52. this.lbl_sec.AutoSize = true;
  53. this.lbl_sec.Font = new System.Drawing.Font("DS-Digital", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  54. this.lbl_sec.ForeColor = System.Drawing.Color.Green;
  55. this.lbl_sec.Location = new System.Drawing.Point(423, 161);
  56. this.lbl_sec.Name = "lbl_sec";
  57. this.lbl_sec.Size = new System.Drawing.Size(57, 40);
  58. this.lbl_sec.TabIndex = 1;
  59. this.lbl_sec.Text = "00";
  60. //
  61. // lbl_day
  62. //
  63. this.lbl_day.AutoSize = true;
  64. this.lbl_day.Font = new System.Drawing.Font("DS-Digital", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  65. this.lbl_day.ForeColor = System.Drawing.Color.Green;
  66. this.lbl_day.Location = new System.Drawing.Point(343, 201);
  67. this.lbl_day.Name = "lbl_day";
  68. this.lbl_day.Size = new System.Drawing.Size(77, 40);
  69. this.lbl_day.TabIndex = 2;
  70. this.lbl_day.Text = "Day";
  71. //
  72. // timer1
  73. //
  74. this.timer1.Enabled = true;
  75. this.timer1.Tick += new System.EventHandler(this.Time_Start);
  76. //
  77. // lbl_date
  78. //
  79. this.lbl_date.AutoSize = true;
  80. this.lbl_date.Font = new System.Drawing.Font("DS-Digital", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  81. this.lbl_date.ForeColor = System.Drawing.Color.Green;
  82. this.lbl_date.Location = new System.Drawing.Point(105, 200);
  83. this.lbl_date.Name = "lbl_date";
  84. this.lbl_date.Size = new System.Drawing.Size(171, 40);
  85. this.lbl_date.TabIndex = 3;
  86. this.lbl_date.Text = "MM-DD-YY";
  87. //
  88. // Main
  89. //
  90. this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
  91. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  92. this.BackColor = System.Drawing.Color.Black;
  93. this.ClientSize = new System.Drawing.Size(557, 342);
  94. this.Controls.Add(this.lbl_date);
  95. this.Controls.Add(this.lbl_day);
  96. this.Controls.Add(this.lbl_sec);
  97. this.Controls.Add(this.lbl_time);
  98. this.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
  99. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
  100. this.Name = "Main";
  101. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  102. this.Text = "Form1";
  103. this.ResumeLayout(false);
  104. this.PerformLayout();
  105.  
  106. }
  107.  
  108. #endregion
  109.  
  110. private System.Windows.Forms.Label lbl_time;
  111. private System.Windows.Forms.Label lbl_sec;
  112. private System.Windows.Forms.Label lbl_day;
  113. private System.Windows.Forms.Timer timer1;
  114. private System.Windows.Forms.Label lbl_date;
  115. }
  116. }

Creating the Script

We will now create the script to make things work. To do first add a timer tools in the form, then go to the timer properties and set enabled to true. tut1 After that go to the csharp script called Main.cs then right click and select view code, this will force you to go to the text editor. Then write these block of codes inside the Class of the form.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Simple_Digital_Clock
  12. {
  13. public partial class Main : Form
  14. {
  15. public Main()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20. private void Time_Start(object sender, EventArgs e)
  21. {
  22. lbl_time.Text = DateTime.Now.ToString("hh:mm");
  23. lbl_sec.Text = DateTime.Now.ToString("ss");
  24. lbl_day.Text = DateTime.Now.ToString("dddd");
  25. lbl_date.Text = DateTime.Now.ToString("MMM dd yyyy");
  26. }
  27.  
  28. }
  29. }
Try to run the application and see if it works. There you go we successfully created a Simple Digital Clock using C#. I hope that this tutorial help you understand on how to develop an application using C#. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Add new comment