Solved

timer - format

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

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 74

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 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

24 Experts available now in Live!

Get 1:1 Help Now