Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do I create a label thats display the time like a digital clock ?

How do I create a label thats display the time like a digital clock ?

Scheduled Pinned Locked Moved C#
question
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lim Yuxuan
    wrote on last edited by
    #1

    My code : private void Form1_Load(object sender, EventArgs e) { winFormTimerLabel.Text = System.DateTime.Now.ToLongTimeString(); } This causes the label to display the current time when the form loads. However, the time does not change. What should I do in order to make the label display the time like a digital clock (with time running)?

    P P M L N 5 Replies Last reply
    0
    • L Lim Yuxuan

      My code : private void Form1_Load(object sender, EventArgs e) { winFormTimerLabel.Text = System.DateTime.Now.ToLongTimeString(); } This causes the label to display the current time when the form loads. However, the time does not change. What should I do in order to make the label display the time like a digital clock (with time running)?

      P Online
      P Online
      PIEBALDconsult
      wrote on last edited by
      #2

      You'll need a thread. I suggest using a System.Timers.Timer.

      1 Reply Last reply
      0
      • L Lim Yuxuan

        My code : private void Form1_Load(object sender, EventArgs e) { winFormTimerLabel.Text = System.DateTime.Now.ToLongTimeString(); } This causes the label to display the current time when the form loads. However, the time does not change. What should I do in order to make the label display the time like a digital clock (with time running)?

        P Offline
        P Offline
        Paul Conrad
        wrote on last edited by
        #3

        There are digital like text/label controls out there. I suggest you do a basic search on such thing.

        "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

        1 Reply Last reply
        0
        • L Lim Yuxuan

          My code : private void Form1_Load(object sender, EventArgs e) { winFormTimerLabel.Text = System.DateTime.Now.ToLongTimeString(); } This causes the label to display the current time when the form loads. However, the time does not change. What should I do in order to make the label display the time like a digital clock (with time running)?

          M Offline
          M Offline
          MarkB777
          wrote on last edited by
          #4

          Hi Lim, If you create a timer that ticks every 1 second, and use the code below in the tick event method: winFormTimerLabel.Text = System.DateTime.UtcNow.TimeOfDay.Hours.ToString() + " : " + System.DateTime.UtcNow.TimeOfDay.Minutes.ToString() + " : " + System.DateTime.UtcNow.TimeOfDay.Seconds.ToString()); I'd imagine it will do what you want. Cheers,

          Mark Brock Click here to view my blog

          P 1 Reply Last reply
          0
          • M MarkB777

            Hi Lim, If you create a timer that ticks every 1 second, and use the code below in the tick event method: winFormTimerLabel.Text = System.DateTime.UtcNow.TimeOfDay.Hours.ToString() + " : " + System.DateTime.UtcNow.TimeOfDay.Minutes.ToString() + " : " + System.DateTime.UtcNow.TimeOfDay.Seconds.ToString()); I'd imagine it will do what you want. Cheers,

            Mark Brock Click here to view my blog

            P Online
            P Online
            PIEBALDconsult
            wrote on last edited by
            #5

            System.DateTime.Now.ToString ( "HH:mm:ss" )

            M 1 Reply Last reply
            0
            • L Lim Yuxuan

              My code : private void Form1_Load(object sender, EventArgs e) { winFormTimerLabel.Text = System.DateTime.Now.ToLongTimeString(); } This causes the label to display the current time when the form loads. However, the time does not change. What should I do in order to make the label display the time like a digital clock (with time running)?

              L Offline
              L Offline
              Lim Yuxuan
              wrote on last edited by
              #6

              Thanks for your efforts guys. In the end this is what I did. private Timer Clock = new Timer();//create a timer void Clock_Tick(object sender, EventArgs e) { //refresh the time every 1 second this.winFormTimerLabel.Text = DateTime.Now.ToLongTimeString(); } private void Form1_Load(object sender, EventArgs e) { Clock.Interval = 1000;//every 1 second do something //what the program is supposed to do when 1 second elasped Clock.Tick += new EventHandler(Clock_Tick); Clock.Start();//starts the timer } I finally understood where did I went wrong - I did not put in the interval value and I did not start the timer..... silly me :laugh:

              P 1 Reply Last reply
              0
              • P PIEBALDconsult

                System.DateTime.Now.ToString ( "HH:mm:ss" )

                M Offline
                M Offline
                MarkB777
                wrote on last edited by
                #7

                Show off :).

                Mark Brock Click here to view my blog

                P 1 Reply Last reply
                0
                • M MarkB777

                  Show off :).

                  Mark Brock Click here to view my blog

                  P Online
                  P Online
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  Ha ha! Ha ha! Mine's smaller! ... Oh, wait... :~

                  1 Reply Last reply
                  0
                  • L Lim Yuxuan

                    Thanks for your efforts guys. In the end this is what I did. private Timer Clock = new Timer();//create a timer void Clock_Tick(object sender, EventArgs e) { //refresh the time every 1 second this.winFormTimerLabel.Text = DateTime.Now.ToLongTimeString(); } private void Form1_Load(object sender, EventArgs e) { Clock.Interval = 1000;//every 1 second do something //what the program is supposed to do when 1 second elasped Clock.Tick += new EventHandler(Clock_Tick); Clock.Start();//starts the timer } I finally understood where did I went wrong - I did not put in the interval value and I did not start the timer..... silly me :laugh:

                    P Online
                    P Online
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    Yup, there you go.

                    1 Reply Last reply
                    0
                    • L Lim Yuxuan

                      My code : private void Form1_Load(object sender, EventArgs e) { winFormTimerLabel.Text = System.DateTime.Now.ToLongTimeString(); } This causes the label to display the current time when the form loads. However, the time does not change. What should I do in order to make the label display the time like a digital clock (with time running)?

                      N Offline
                      N Offline
                      nelsonpaixao
                      wrote on last edited by
                      #10

                      keep the code above, just add a timer object!!! edit event tick, and interval propriety. This will trigger your above code all the time!!! The timer will be refreshed. :-D

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups