Clock

How would I code a live simple 12 hour format clock that keeps up with my system's clock on my program with "hours, minutes, and seconds" on a status strip on the bottom right


Answer this question

Clock

  • RLewis

    Oh, sorry, you're using C++. Are you using managed C++ If so, you have access to the datetime class. You'd also have access to the Timer class.

    System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();

    t.Tick += new EventHandler(clockTick);

    t.Interval = 500;

    t.Start();

    private void clockTick(object sender, EventArgs ea)

    {

    string time = DateTime.Now.ToShortTimeString();

    }

    That's C#, first some code in your form load event to create a timer ( actually, it should be a member variable ), and then an event that creates a time string, which you can then put on the status bar. I'm not sure how you format where it appears.

    I'd have to know if you're using .NET in your code, if not, the answer is the same conceptually, but the classes you use are totally different. I also don't use C++/CLI, which is why I answered in C#, sorry.



  • Mohamed Shifaz

    http://www.codersource.net/mfc_working_with_timers.html

    That's an article on using timers. WM_TIMER is probably what you want to type into your search engine for more info, if you need it

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/vclib/html/_CRT_localtime.asp

    This MSDN article shows you how to format the system time into a string.

    You need a slight modification of the code in the second link ( to remove the date part ) to run inside a timer event, and then to assign that string to your status bar.



  • Youpas

    Use a timer to refresh your clock, and use DateTime.Now to get the current system time.



  • Markish

    I know that I'm new at all of this but could you give better details, I know what datetime.now is but I don't now how to use it or the timer, and whenever I put something in a statusstrip it stays on the left side and I don't know how to move it to the right or the middle and that sort of thing.

  • JaYZhiE

    I am using managed C++. But I do need it to be coded in C++ if that's ok.

  • DebODiver

    Because that's twice a second, which means that even if the timer doesn't fire exactly on time ( and they are not guarenteed to ) your clock won't ever skip a second.



  • MurrayBrown

    why did you use an interval of 500 milliseconds

  • MrLister

    No WM_TIMER won't work it's managed code.

  • Clock