• We just launched and are currently in beta. Join us as we build and grow the community.

C# - Simple Digital Clock

bonestherapper

Product Launch Expert
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
108
Likes
169
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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. /// <summary>
  10. /// Clean up any resources being used.
  11. /// </summary>
  12. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  13. protected

    override

    void

    Dispose(

    bool

    disposing)
  14. {
  15. if

    (

    disposing &&

    (

    components !=

    null

    )

    )
  16. {
  17. components.

    Dispose

    (

    )

    ;
  18. }
  19. base

    .

    Dispose

    (

    disposing)

    ;
  20. }

  21. #region Windows Form Designer generated code

  22. /// <summary>
  23. /// Required method for Designer support - do not modify
  24. /// the contents of this method with the code editor.
  25. /// </summary>
  26. private

    void

    InitializeComponent(

    )
  27. {
  28. this

    .

    components

    =

    new

    System.ComponentModel

    .

    Container

    (

    )

    ;
  29. this

    .

    lbl_time

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    Label

    (

    )

    ;
  30. this

    .

    lbl_sec

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    Label

    (

    )

    ;
  31. this

    .

    lbl_day

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    Label

    (

    )

    ;
  32. this

    .

    timer1

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    Timer

    (

    this

    .

    components

    )

    ;
  33. this

    .

    lbl_date

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    Label

    (

    )

    ;
  34. this

    .

    SuspendLayout

    (

    )

    ;
  35. //
  36. // lbl_time
  37. //
  38. this

    .

    lbl_time

    .

    AutoSize

    =

    true

    ;
  39. this

    .

    lbl_time

    .

    Font

    =

    new

    System.Drawing

    .

    Font

    (

    "DS-Digital"

    , 72F, System.Drawing

    .

    FontStyle

    .

    Regular

    , System.Drawing

    .

    GraphicsUnit

    .

    Point

    , (

    (

    byte

    )

    (

    0

    )

    )

    )

    ;
  40. this

    .

    lbl_time

    .

    ForeColor

    =

    System.Drawing

    .

    Color

    .

    Green

    ;
  41. this

    .

    lbl_time

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    88

    , 76

    )

    ;
  42. this

    .

    lbl_time

    .

    Name

    =

    "lbl_time"

    ;
  43. this

    .

    lbl_time

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    376

    , 142

    )

    ;
  44. this

    .

    lbl_time

    .

    TabIndex

    =

    0

    ;
  45. this

    .

    lbl_time

    .

    Text

    =

    "00:00"

    ;
  46. //
  47. // lbl_sec
  48. //
  49. this

    .

    lbl_sec

    .

    AutoSize

    =

    true

    ;
  50. this

    .

    lbl_sec

    .

    Font

    =

    new

    System.Drawing

    .

    Font

    (

    "DS-Digital"

    , 20F, System.Drawing

    .

    FontStyle

    .

    Regular

    , System.Drawing

    .

    GraphicsUnit

    .

    Point

    , (

    (

    byte

    )

    (

    0

    )

    )

    )

    ;
  51. this

    .

    lbl_sec

    .

    ForeColor

    =

    System.Drawing

    .

    Color

    .

    Green

    ;
  52. this

    .

    lbl_sec

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    423

    , 161

    )

    ;
  53. this

    .

    lbl_sec

    .

    Name

    =

    "lbl_sec"

    ;
  54. this

    .

    lbl_sec

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    57

    , 40

    )

    ;
  55. this

    .

    lbl_sec

    .

    TabIndex

    =

    1

    ;
  56. this

    .

    lbl_sec

    .

    Text

    =

    "00"

    ;
  57. //
  58. // lbl_day
  59. //
  60. this

    .

    lbl_day

    .

    AutoSize

    =

    true

    ;
  61. this

    .

    lbl_day

    .

    Font

    =

    new

    System.Drawing

    .

    Font

    (

    "DS-Digital"

    , 20F, System.Drawing

    .

    FontStyle

    .

    Regular

    , System.Drawing

    .

    GraphicsUnit

    .

    Point

    , (

    (

    byte

    )

    (

    0

    )

    )

    )

    ;
  62. this

    .

    lbl_day

    .

    ForeColor

    =

    System.Drawing

    .

    Color

    .

    Green

    ;
  63. this

    .

    lbl_day

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    343

    , 201

    )

    ;
  64. this

    .

    lbl_day

    .

    Name

    =

    "lbl_day"

    ;
  65. this

    .

    lbl_day

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    77

    , 40

    )

    ;
  66. this

    .

    lbl_day

    .

    TabIndex

    =

    2

    ;
  67. this

    .

    lbl_day

    .

    Text

    =

    "Day"

    ;
  68. //
  69. // timer1
  70. //
  71. this

    .

    timer1

    .

    Enabled

    =

    true

    ;
  72. this

    .

    timer1

    .

    Tick

    +=

    new

    System

    .

    EventHandler

    (

    this

    .

    Time_Start

    )

    ;
  73. //
  74. // lbl_date
  75. //
  76. this

    .

    lbl_date

    .

    AutoSize

    =

    true

    ;
  77. this

    .

    lbl_date

    .

    Font

    =

    new

    System.Drawing

    .

    Font

    (

    "DS-Digital"

    , 20F, System.Drawing

    .

    FontStyle

    .

    Regular

    , System.Drawing

    .

    GraphicsUnit

    .

    Point

    , (

    (

    byte

    )

    (

    0

    )

    )

    )

    ;
  78. this

    .

    lbl_date

    .

    ForeColor

    =

    System.Drawing

    .

    Color

    .

    Green

    ;
  79. this

    .

    lbl_date

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    105

    , 200

    )

    ;
  80. this

    .

    lbl_date

    .

    Name

    =

    "lbl_date"

    ;
  81. this

    .

    lbl_date

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    171

    , 40

    )

    ;
  82. this

    .

    lbl_date

    .

    TabIndex

    =

    3

    ;
  83. this

    .

    lbl_date

    .

    Text

    =

    "MM-DD-YY"

    ;
  84. //
  85. // Main
  86. //
  87. this

    .

    AutoScaleDimensions

    =

    new

    System.Drawing

    .

    SizeF

    (

    9F, 20F)

    ;
  88. this

    .

    AutoScaleMode

    =

    System

    .

    Windows

    .

    Forms

    .

    AutoScaleMode

    .

    Font

    ;
  89. this

    .

    BackColor

    =

    System.Drawing

    .

    Color

    .

    Black

    ;
  90. this

    .

    ClientSize

    =

    new

    System.Drawing

    .

    Size

    (

    557

    , 342

    )

    ;
  91. this

    .

    Controls

    .

    Add

    (

    this

    .

    lbl_date

    )

    ;
  92. this

    .

    Controls

    .

    Add

    (

    this

    .

    lbl_day

    )

    ;
  93. this

    .

    Controls

    .

    Add

    (

    this

    .

    lbl_sec

    )

    ;
  94. this

    .

    Controls

    .

    Add

    (

    this

    .

    lbl_time

    )

    ;
  95. this

    .

    ForeColor

    =

    System.Drawing

    .

    SystemColors

    .

    ActiveCaptionText

    ;
  96. this

    .

    FormBorderStyle

    =

    System

    .

    Windows

    .

    Forms

    .

    FormBorderStyle

    .

    None

    ;
  97. this

    .

    Name

    =

    "Main"

    ;
  98. this

    .

    StartPosition

    =

    System

    .

    Windows

    .

    Forms

    .

    FormStartPosition

    .

    CenterScreen

    ;
  99. this

    .

    Text

    =

    "Form1"

    ;
  100. this

    .

    ResumeLayout

    (

    false

    )

    ;
  101. this

    .

    PerformLayout

    (

    )

    ;

  102. }

  103. #endregion

  104. private

    System

    .

    Windows

    .

    Forms

    .

    Label

    lbl_time;
  105. private

    System

    .

    Windows

    .

    Forms

    .

    Label

    lbl_sec;
  106. private

    System

    .

    Windows

    .

    Forms

    .

    Label

    lbl_day;
  107. private

    System

    .

    Windows

    .

    Forms

    .

    Timer

    timer1;
  108. private

    System

    .

    Windows

    .

    Forms

    .

    Label

    lbl_date;
  109. }
  110. }

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.
2018-06-04_16_36_04-simple_digital_clock_-_microsoft_visual_studio.png

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. namespace

    Simple_Digital_Clock
  11. {
  12. public

    partial

    class

    Main :

    Form
  13. {
  14. public

    Main(

    )
  15. {
  16. InitializeComponent(

    )

    ;
  17. }

  18. private

    void

    Time_Start(

    object

    sender, EventArgs e)
  19. {
  20. lbl_time.

    Text

    =

    DateTime.

    Now

    .

    ToString

    (

    "hh:mm"

    )

    ;
  21. lbl_sec.

    Text

    =

    DateTime.

    Now

    .

    ToString

    (

    "ss"

    )

    ;
  22. lbl_day.

    Text

    =

    DateTime.

    Now

    .

    ToString

    (

    "dddd"

    )

    ;
  23. lbl_date.

    Text

    =

    DateTime.

    Now

    .

    ToString

    (

    "MMM dd yyyy"

    )

    ;
  24. }

  25. }
  26. }

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!!!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top