Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

timer - format

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
VBdotnet2005
Asked:
VBdotnet2005
  • 3
  • 2
2 Solutions
 
käµfm³d 👽Commented:
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
 
VBdotnet2005Author Commented:
I understand what you meant, but not sure how.
textBox2.Text = t.seconds ++ 1   or timespan ++1 ?  like such?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
käµfm³d 👽Commented:
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
 
VBdotnet2005Author Commented:
Idle_Mind,

This is exactly what I need. Thank you much :)
0
 
VBdotnet2005Author Commented:
kaufmed,

Thank you very much also.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now