Hello There, Guest!
View New Posts  |  View Today's Posts
[C#] DateTimePicker Auto Update?

  • 0 Vote(s) - 0 Average


01-16-2014, 04:18 PM #1
Lee Stevens
Senior Member
***
Posts: 345 Threads:54 Joined: Feb 2012 Reputation: 12

DateTimePicker Auto Update?
Hi All,

Quick question i have a DateTimePicker that displays just the date, for reporting. Is there a way to set it to auto update on the following day?

So if the i open the app on Tuesday and it stays open till Wednesday that the app changes the dates it's self.

Cheers,
Lee.

01-16-2014, 05:24 PM #2
Miklo
Junior Member
**
Posts: 39 Threads:6 Joined: Feb 2013 Reputation: 0

RE: DateTimePicker Auto Update?
:( Sorry I cant answer that when it comes to C#. But I am positive with a bit of coding it can be done. Good luck with it !
This post was last modified: 01-16-2014, 05:31 PM by Miklo.

01-16-2014, 07:31 PM #3
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,862 Threads:494 Joined: Jun 2011 Reputation: 67

RE: DateTimePicker Auto Update?
Something like this should do the trick:
Code:
static class MidnightNotifier
{
    private static readonly Timer timer;

    static MidnightNotifier()
    {
        timer = new Timer(GetSleepTime());
        timer.Elapsed += (s, e) =>
        {
            OnDayChanged();
            timer.Interval = GetSleepTime();
        };
        timer.Start();

        SystemEvents.TimeChanged += OnSystemTimeChanged;
    }

    private static double GetSleepTime()
    {
        var midnightTonight = DateTime.Today.AddDays(1);
        var differenceInMilliseconds = (midnightTonight - DateTime.Now).TotalMilliseconds;
        return differenceInMilliseconds;
    }

    private static void OnDayChanged()
    {
        var handler = DayChanged;
        if (handler != null)
            handler(null, null);
    }

    private static void OnSystemTimeChanged(object sender, EventArgs e)
    {
        timer.Interval = GetSleepTime();
    }

    public static event EventHandler<EventArgs> DayChanged;
}

To use:
Code:
MidnightNotifier.DayChanged += (s, e) => { Console.WriteLine("The Day Changed!"); };


Source: http://stackoverflow.com/questions/84800...t-midnight


That code could be written a bit nicer, but you get the idea. Just listen on that event.. when it's fired.. update your date. :D
This post was last modified: 01-16-2014, 07:33 PM by KoBE.

01-16-2014, 07:47 PM #4
Lee Stevens
Senior Member
***
Posts: 345 Threads:54 Joined: Feb 2012 Reputation: 12

RE: DateTimePicker Auto Update?
Quality KoBE! Thank you very much!

01-17-2014, 10:58 AM #5
Miklo
Junior Member
**
Posts: 39 Threads:6 Joined: Feb 2013 Reputation: 0

RE: DateTimePicker Auto Update?
Well done Kobe! I cant +Rep becuase I already did, else I would for your continiously help!




Forum Jump:


Possibly Related Threads...
Thread Author Replies Views Last Post
  Lync 2010 Auto Responder KoBE 1 1,696 06-08-2015, 10:49 AM
Last Post: KoBE
   Screenshot Utility with Auto Upload KoBE 12 7,385 08-28-2014, 08:12 PM
Last Post: KoBE
   [Tutorial]Auto Update Tutorial for VB.Net Gemster 8 18,077 02-08-2014, 04:34 AM
Last Post: assasaha
   Update Form from Class Thread Lee Stevens 2 2,410 10-13-2013, 05:50 PM
Last Post: Lee Stevens
  Mysql insert error or update t0kneneng 13 6,150 06-03-2013, 10:24 PM
Last Post: t0kneneng


Users browsing this thread: 1 Guest(s)