How can i make a countdown timer in C# that accounts for hours, minutes and seconds? [duplicate]












1
















This question already has an answer here:




  • How can I String.Format a TimeSpan object with a custom format in .NET?

    19 answers




I am not a good programmer, it's rather my hobby.
So please don't judge me for being bad at programming. This is my current code ive made for the countdown timer.



int i = 000000;
private void timer1_Tick(object sender, EventArgs e)
{
{
i++;
textBox1.Text = i.ToString() + "00:00:00";
}
}


When I run the timer. then the TextBox1 isn't showing 00:00:01, but it's showing 100;00;00.
Thank you for reading my post and again, sorry for having soo bad programming knowledge.










share|improve this question















marked as duplicate by mjwills, Community Jan 21 at 16:57


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    1
















    This question already has an answer here:




    • How can I String.Format a TimeSpan object with a custom format in .NET?

      19 answers




    I am not a good programmer, it's rather my hobby.
    So please don't judge me for being bad at programming. This is my current code ive made for the countdown timer.



    int i = 000000;
    private void timer1_Tick(object sender, EventArgs e)
    {
    {
    i++;
    textBox1.Text = i.ToString() + "00:00:00";
    }
    }


    When I run the timer. then the TextBox1 isn't showing 00:00:01, but it's showing 100;00;00.
    Thank you for reading my post and again, sorry for having soo bad programming knowledge.










    share|improve this question















    marked as duplicate by mjwills, Community Jan 21 at 16:57


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      1












      1








      1









      This question already has an answer here:




      • How can I String.Format a TimeSpan object with a custom format in .NET?

        19 answers




      I am not a good programmer, it's rather my hobby.
      So please don't judge me for being bad at programming. This is my current code ive made for the countdown timer.



      int i = 000000;
      private void timer1_Tick(object sender, EventArgs e)
      {
      {
      i++;
      textBox1.Text = i.ToString() + "00:00:00";
      }
      }


      When I run the timer. then the TextBox1 isn't showing 00:00:01, but it's showing 100;00;00.
      Thank you for reading my post and again, sorry for having soo bad programming knowledge.










      share|improve this question

















      This question already has an answer here:




      • How can I String.Format a TimeSpan object with a custom format in .NET?

        19 answers




      I am not a good programmer, it's rather my hobby.
      So please don't judge me for being bad at programming. This is my current code ive made for the countdown timer.



      int i = 000000;
      private void timer1_Tick(object sender, EventArgs e)
      {
      {
      i++;
      textBox1.Text = i.ToString() + "00:00:00";
      }
      }


      When I run the timer. then the TextBox1 isn't showing 00:00:01, but it's showing 100;00;00.
      Thank you for reading my post and again, sorry for having soo bad programming knowledge.





      This question already has an answer here:




      • How can I String.Format a TimeSpan object with a custom format in .NET?

        19 answers








      c#






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 19 at 9:36









      Prashant Pimpale

      3,2573830




      3,2573830










      asked Jan 19 at 9:31









      Jaan SuurJaan Suur

      45




      45




      marked as duplicate by mjwills, Community Jan 21 at 16:57


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by mjwills, Community Jan 21 at 16:57


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          3 Answers
          3






          active

          oldest

          votes


















          4














          You can use this:



          TimeSpan time = TimeSpan.FromSeconds(i);
          textBox1.Text = time.ToString(@"hh:mm:ss");





          share|improve this answer































            0














            Ok, so you know how to use Timer but your problem is with string, when you use + on string you are concatenating them, so say "hello" + " world" is equal to "hello world", so when you concat i with 00:00:00 the output you see is very logical.

            you can use snippet below to achieve your goal(just replace your form class content with this)



            private Timer _timer;
            private Label _label;
            private int _elapsedSeconds;
            public Form1()
            {
            _timer = new Timer
            {
            Interval = 1000,
            Enabled = true
            };
            _timer.Tick += (sender, args) =>
            {
            _elapsedSeconds++;
            TimeSpan time = TimeSpan.FromSeconds(_elapsedSeconds);
            _label.Text = time.ToString(@"hh:mm:ss");
            };

            _label = new Label();

            Controls.Add(_label);
            }


            Edit



            Tnx to @Juliet Wilson I edited how time is converted to string






            share|improve this answer


























            • Though you start off with a nice description of the immediate problem, your code solution is kinda convuluted. Why not use ToString() as per Juliet's answer instead of the fidly hour/min/sec calculation?

              – MickyD
              Jan 19 at 10:00













            • @shash678, tnx for the link, but my naming convention is taken from Jetbrain reharper

              – hessam hedieh
              Jan 19 at 10:05











            • downvote removed. thanks for improving your answer :)

              – MickyD
              Jan 19 at 10:06





















            -1














            I would suggest to use Stopwatch for that. It will do the magic you need. Here you can find good example of using Stopwatch class in C# https://www.dotnetperls.com/stopwatch.






            share|improve this answer
























            • Please mark it as answer as it resolved your problem.

              – Gaurav
              Jan 19 at 9:54











            • Well no, not only wouldn't address the immediate problem (formatting), StopWatch doesn't have any count-down ability at all.

              – MickyD
              Jan 19 at 9:58











            • @MickyD, although user has stated countdown in question, but looking at code he implied something else

              – hessam hedieh
              Jan 19 at 10:00











            • @hessamhedieh That's true. The answer is borderline comment with a link to a foreign site. Perhaps if author improves answer I'll upvote :)

              – MickyD
              Jan 19 at 10:05


















            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4














            You can use this:



            TimeSpan time = TimeSpan.FromSeconds(i);
            textBox1.Text = time.ToString(@"hh:mm:ss");





            share|improve this answer




























              4














              You can use this:



              TimeSpan time = TimeSpan.FromSeconds(i);
              textBox1.Text = time.ToString(@"hh:mm:ss");





              share|improve this answer


























                4












                4








                4







                You can use this:



                TimeSpan time = TimeSpan.FromSeconds(i);
                textBox1.Text = time.ToString(@"hh:mm:ss");





                share|improve this answer













                You can use this:



                TimeSpan time = TimeSpan.FromSeconds(i);
                textBox1.Text = time.ToString(@"hh:mm:ss");






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 19 at 9:57









                Juliet WilsonJuliet Wilson

                561




                561

























                    0














                    Ok, so you know how to use Timer but your problem is with string, when you use + on string you are concatenating them, so say "hello" + " world" is equal to "hello world", so when you concat i with 00:00:00 the output you see is very logical.

                    you can use snippet below to achieve your goal(just replace your form class content with this)



                    private Timer _timer;
                    private Label _label;
                    private int _elapsedSeconds;
                    public Form1()
                    {
                    _timer = new Timer
                    {
                    Interval = 1000,
                    Enabled = true
                    };
                    _timer.Tick += (sender, args) =>
                    {
                    _elapsedSeconds++;
                    TimeSpan time = TimeSpan.FromSeconds(_elapsedSeconds);
                    _label.Text = time.ToString(@"hh:mm:ss");
                    };

                    _label = new Label();

                    Controls.Add(_label);
                    }


                    Edit



                    Tnx to @Juliet Wilson I edited how time is converted to string






                    share|improve this answer


























                    • Though you start off with a nice description of the immediate problem, your code solution is kinda convuluted. Why not use ToString() as per Juliet's answer instead of the fidly hour/min/sec calculation?

                      – MickyD
                      Jan 19 at 10:00













                    • @shash678, tnx for the link, but my naming convention is taken from Jetbrain reharper

                      – hessam hedieh
                      Jan 19 at 10:05











                    • downvote removed. thanks for improving your answer :)

                      – MickyD
                      Jan 19 at 10:06


















                    0














                    Ok, so you know how to use Timer but your problem is with string, when you use + on string you are concatenating them, so say "hello" + " world" is equal to "hello world", so when you concat i with 00:00:00 the output you see is very logical.

                    you can use snippet below to achieve your goal(just replace your form class content with this)



                    private Timer _timer;
                    private Label _label;
                    private int _elapsedSeconds;
                    public Form1()
                    {
                    _timer = new Timer
                    {
                    Interval = 1000,
                    Enabled = true
                    };
                    _timer.Tick += (sender, args) =>
                    {
                    _elapsedSeconds++;
                    TimeSpan time = TimeSpan.FromSeconds(_elapsedSeconds);
                    _label.Text = time.ToString(@"hh:mm:ss");
                    };

                    _label = new Label();

                    Controls.Add(_label);
                    }


                    Edit



                    Tnx to @Juliet Wilson I edited how time is converted to string






                    share|improve this answer


























                    • Though you start off with a nice description of the immediate problem, your code solution is kinda convuluted. Why not use ToString() as per Juliet's answer instead of the fidly hour/min/sec calculation?

                      – MickyD
                      Jan 19 at 10:00













                    • @shash678, tnx for the link, but my naming convention is taken from Jetbrain reharper

                      – hessam hedieh
                      Jan 19 at 10:05











                    • downvote removed. thanks for improving your answer :)

                      – MickyD
                      Jan 19 at 10:06
















                    0












                    0








                    0







                    Ok, so you know how to use Timer but your problem is with string, when you use + on string you are concatenating them, so say "hello" + " world" is equal to "hello world", so when you concat i with 00:00:00 the output you see is very logical.

                    you can use snippet below to achieve your goal(just replace your form class content with this)



                    private Timer _timer;
                    private Label _label;
                    private int _elapsedSeconds;
                    public Form1()
                    {
                    _timer = new Timer
                    {
                    Interval = 1000,
                    Enabled = true
                    };
                    _timer.Tick += (sender, args) =>
                    {
                    _elapsedSeconds++;
                    TimeSpan time = TimeSpan.FromSeconds(_elapsedSeconds);
                    _label.Text = time.ToString(@"hh:mm:ss");
                    };

                    _label = new Label();

                    Controls.Add(_label);
                    }


                    Edit



                    Tnx to @Juliet Wilson I edited how time is converted to string






                    share|improve this answer















                    Ok, so you know how to use Timer but your problem is with string, when you use + on string you are concatenating them, so say "hello" + " world" is equal to "hello world", so when you concat i with 00:00:00 the output you see is very logical.

                    you can use snippet below to achieve your goal(just replace your form class content with this)



                    private Timer _timer;
                    private Label _label;
                    private int _elapsedSeconds;
                    public Form1()
                    {
                    _timer = new Timer
                    {
                    Interval = 1000,
                    Enabled = true
                    };
                    _timer.Tick += (sender, args) =>
                    {
                    _elapsedSeconds++;
                    TimeSpan time = TimeSpan.FromSeconds(_elapsedSeconds);
                    _label.Text = time.ToString(@"hh:mm:ss");
                    };

                    _label = new Label();

                    Controls.Add(_label);
                    }


                    Edit



                    Tnx to @Juliet Wilson I edited how time is converted to string







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 19 at 10:02

























                    answered Jan 19 at 9:57









                    hessam hediehhessam hedieh

                    656412




                    656412













                    • Though you start off with a nice description of the immediate problem, your code solution is kinda convuluted. Why not use ToString() as per Juliet's answer instead of the fidly hour/min/sec calculation?

                      – MickyD
                      Jan 19 at 10:00













                    • @shash678, tnx for the link, but my naming convention is taken from Jetbrain reharper

                      – hessam hedieh
                      Jan 19 at 10:05











                    • downvote removed. thanks for improving your answer :)

                      – MickyD
                      Jan 19 at 10:06





















                    • Though you start off with a nice description of the immediate problem, your code solution is kinda convuluted. Why not use ToString() as per Juliet's answer instead of the fidly hour/min/sec calculation?

                      – MickyD
                      Jan 19 at 10:00













                    • @shash678, tnx for the link, but my naming convention is taken from Jetbrain reharper

                      – hessam hedieh
                      Jan 19 at 10:05











                    • downvote removed. thanks for improving your answer :)

                      – MickyD
                      Jan 19 at 10:06



















                    Though you start off with a nice description of the immediate problem, your code solution is kinda convuluted. Why not use ToString() as per Juliet's answer instead of the fidly hour/min/sec calculation?

                    – MickyD
                    Jan 19 at 10:00







                    Though you start off with a nice description of the immediate problem, your code solution is kinda convuluted. Why not use ToString() as per Juliet's answer instead of the fidly hour/min/sec calculation?

                    – MickyD
                    Jan 19 at 10:00















                    @shash678, tnx for the link, but my naming convention is taken from Jetbrain reharper

                    – hessam hedieh
                    Jan 19 at 10:05





                    @shash678, tnx for the link, but my naming convention is taken from Jetbrain reharper

                    – hessam hedieh
                    Jan 19 at 10:05













                    downvote removed. thanks for improving your answer :)

                    – MickyD
                    Jan 19 at 10:06







                    downvote removed. thanks for improving your answer :)

                    – MickyD
                    Jan 19 at 10:06













                    -1














                    I would suggest to use Stopwatch for that. It will do the magic you need. Here you can find good example of using Stopwatch class in C# https://www.dotnetperls.com/stopwatch.






                    share|improve this answer
























                    • Please mark it as answer as it resolved your problem.

                      – Gaurav
                      Jan 19 at 9:54











                    • Well no, not only wouldn't address the immediate problem (formatting), StopWatch doesn't have any count-down ability at all.

                      – MickyD
                      Jan 19 at 9:58











                    • @MickyD, although user has stated countdown in question, but looking at code he implied something else

                      – hessam hedieh
                      Jan 19 at 10:00











                    • @hessamhedieh That's true. The answer is borderline comment with a link to a foreign site. Perhaps if author improves answer I'll upvote :)

                      – MickyD
                      Jan 19 at 10:05
















                    -1














                    I would suggest to use Stopwatch for that. It will do the magic you need. Here you can find good example of using Stopwatch class in C# https://www.dotnetperls.com/stopwatch.






                    share|improve this answer
























                    • Please mark it as answer as it resolved your problem.

                      – Gaurav
                      Jan 19 at 9:54











                    • Well no, not only wouldn't address the immediate problem (formatting), StopWatch doesn't have any count-down ability at all.

                      – MickyD
                      Jan 19 at 9:58











                    • @MickyD, although user has stated countdown in question, but looking at code he implied something else

                      – hessam hedieh
                      Jan 19 at 10:00











                    • @hessamhedieh That's true. The answer is borderline comment with a link to a foreign site. Perhaps if author improves answer I'll upvote :)

                      – MickyD
                      Jan 19 at 10:05














                    -1












                    -1








                    -1







                    I would suggest to use Stopwatch for that. It will do the magic you need. Here you can find good example of using Stopwatch class in C# https://www.dotnetperls.com/stopwatch.






                    share|improve this answer













                    I would suggest to use Stopwatch for that. It will do the magic you need. Here you can find good example of using Stopwatch class in C# https://www.dotnetperls.com/stopwatch.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 19 at 9:33









                    GauravGaurav

                    58637




                    58637













                    • Please mark it as answer as it resolved your problem.

                      – Gaurav
                      Jan 19 at 9:54











                    • Well no, not only wouldn't address the immediate problem (formatting), StopWatch doesn't have any count-down ability at all.

                      – MickyD
                      Jan 19 at 9:58











                    • @MickyD, although user has stated countdown in question, but looking at code he implied something else

                      – hessam hedieh
                      Jan 19 at 10:00











                    • @hessamhedieh That's true. The answer is borderline comment with a link to a foreign site. Perhaps if author improves answer I'll upvote :)

                      – MickyD
                      Jan 19 at 10:05



















                    • Please mark it as answer as it resolved your problem.

                      – Gaurav
                      Jan 19 at 9:54











                    • Well no, not only wouldn't address the immediate problem (formatting), StopWatch doesn't have any count-down ability at all.

                      – MickyD
                      Jan 19 at 9:58











                    • @MickyD, although user has stated countdown in question, but looking at code he implied something else

                      – hessam hedieh
                      Jan 19 at 10:00











                    • @hessamhedieh That's true. The answer is borderline comment with a link to a foreign site. Perhaps if author improves answer I'll upvote :)

                      – MickyD
                      Jan 19 at 10:05

















                    Please mark it as answer as it resolved your problem.

                    – Gaurav
                    Jan 19 at 9:54





                    Please mark it as answer as it resolved your problem.

                    – Gaurav
                    Jan 19 at 9:54













                    Well no, not only wouldn't address the immediate problem (formatting), StopWatch doesn't have any count-down ability at all.

                    – MickyD
                    Jan 19 at 9:58





                    Well no, not only wouldn't address the immediate problem (formatting), StopWatch doesn't have any count-down ability at all.

                    – MickyD
                    Jan 19 at 9:58













                    @MickyD, although user has stated countdown in question, but looking at code he implied something else

                    – hessam hedieh
                    Jan 19 at 10:00





                    @MickyD, although user has stated countdown in question, but looking at code he implied something else

                    – hessam hedieh
                    Jan 19 at 10:00













                    @hessamhedieh That's true. The answer is borderline comment with a link to a foreign site. Perhaps if author improves answer I'll upvote :)

                    – MickyD
                    Jan 19 at 10:05





                    @hessamhedieh That's true. The answer is borderline comment with a link to a foreign site. Perhaps if author improves answer I'll upvote :)

                    – MickyD
                    Jan 19 at 10:05



                    Popular posts from this blog

                    Homophylophilia

                    Updating UILabel text programmatically using a function

                    Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage