Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

timer - format

Posted on 2010-11-11
6
Medium Priority
?
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 248 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 252 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

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 shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

660 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