Solved

timer - format

Posted on 2010-11-11
6
293 Views
Last Modified: 2013-12-17
Why wouldn't time tick and display on textbox2?

   TimeSpan t;
        public MainWindow()
        {
            InitializeComponent();
           
            t = TimeSpan.FromSeconds(1);
            tmr2.Interval = t;
            tmr2.Tick += OnTimerTick;
            tmr2.Start();
        }


   private void button6_Click(object sender, RoutedEventArgs e)
        {
            tmr2.Stop();
            countup = 0;
            tmr2.Start();
        }

  void OnTimerTick(object sender, EventArgs args)
        {

            textBox2.Text = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms",  
                        t.Hours,  
                        t.Minutes,  
                        t.Seconds,  
                        t.Milliseconds);
           
        }
0
Comment
Question by:VBdotnet2005
  • 3
  • 2
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34115599
Because you are displaying the class-level TimeSpan that you set in the constructor, but you never actually change its value after that. Every timer tick, you are displaying the same exact value. Try modifying its value whenever a tick occurs, then display it.

The example I posted in your last question should demonstreate what I mean. Instead of using an int like I did, alter the logic to work with a TimeSpan instead.
0
 

Author Comment

by:VBdotnet2005
ID: 34116390
I understand what you meant, but not sure how.
textBox2.Text = t.seconds ++ 1   or timespan ++1 ?  like such?
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 62 total points
ID: 34117233
You can use the Stopwatch() class and simply access the Elapsed() property in the Tick() event of your Timer...

Something like:
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();



        public MainWindow()

        {

            InitializeComponent();



            sw.Start();

            tmr2.Interval = 1000;

            tmr2.Tick += OnTimerTick;

            tmr2.Start();

        }



        void OnTimerTick(object sender, EventArgs args)

        {

            textBox2.Text = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms",

                        sw.Elapsed.Hours,

                        sw.Elapsed.Minutes,

                        sw.Elapsed.Seconds,

                        sw.Elapsed.Milliseconds);



        }



        private void button6_Click(object sender, RoutedEventArgs e)

        {

            sw.Reset();

            sw.Start();

        }

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 63 total points
ID: 34120137
Modify your tick handler as such:
void OnTimerTick(object sender, EventArgs args)
{
    textBox2.Text = string.Format("{0:D2}h:{1:D2}m:{2:D2}s{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds);
    t = t.Add(new TimeSpan(0, 0, 1));  // Add one second; you can change it to -1 to subtract one second
}

Open in new window

0
 

Author Comment

by:VBdotnet2005
ID: 34120516
Idle_Mind,

This is exactly what I need. Thank you much :)
0
 

Author Comment

by:VBdotnet2005
ID: 34120530
kaufmed,

Thank you very much also.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now