Solved

timer - format

Posted on 2010-11-11
6
294 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SqlDataBase 7 48
Modal Popup Extender control 1 20
VB.Net WebRequest Code from JSON curl 9 13
Expression Evaluater 3 26
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

778 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