0

having a lot of frustrations with DateTimePickers... First, let me explain my setup. I have 2 pairs of DateTimePickers. 1 is From Date (formatted MM/dd/yyyy), and its pair is From Hour (formatted HH and ShowUpDown = true). The other pair is identical save for its the To Date and To Hour. There's a couple of rules they need to obey.

  1. The From pickers MaxDates can never be more than the To pickers MaxDates
  2. If the To pickers selected day (ToDatePicker) is the current day, then both their MaxDates needs to be DateTime.Now.AddHours(1). This way the user will be able to search the "current" hour... ex: if its currently 4/2/13 10:18 PM, MaxDate for the To picker pair needs to be 4/2/13 11:00 PM, so the user can set Value of From picker pair to 4/2/13 10 PM and To picker pair to 4/2/13 11 PM, allowing them to search the 18 minutes of 10 pm hour.

Im sure theres more but I'm exhausted... I think yall get the idea. I've tried to accomplish this with some tricky DataBindings like:

In Constructor:

FromDatePicker.DataBindings.Add(new Binding("MaxDate", ToHourPicker, "MaxDate"));
ToDatePicker.DataBindings.Add(new Binding("MaxDate", ToHourPicker, "MaxDate"));
FromHourPicker.DataBindings.Add(new Binding("Value", FromDatePicker, "Value"));
ToHourPicker.DataBindings.Add(new Binding("Value", ToDatePicker, "Value"));

DateTime dt = DateTime.Now.Subtract(new Timespan(0, 0, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond));
DateTime plusOne = dt.AddHours(1);

FromDatePicker.MaxDate = dt;
FromHourPicker.MaxDate = dt;
ToDatePicker.MaxDate = plusOne;
ToHourPicker.MaxDate = plusOne;

In MainForm Load Event

FromDatePicker.Value = FromDatePicker.MaxDate;
FromHourPicker.Value = FromDatePicker.MaxDate;
ToDatePicker.Value = ToDatePicker.MaxDate;
ToHourPicker.Value = ToHourPicker.MaxDate;

With that setup it actually fails on the FromDatePicker.Value = FromDatePicker.MaxDate; line with the error message of Value of <current day & hour> is not valid. Value should be between MinDate and MaxDate, except when I mouse over FromDatePicker in that very line it says MaxDate is that same <current day & hour>....

I'm super confused and open to improving what I got or if you guys can think of a simpler way to achieve the same effect I'd obviously love to hear those suggestions too :P. Let me know if you need more

Hershizer33
  • 1,206
  • 2
  • 23
  • 46
  • have you tried something like this `DateTime date = FromDatePicker.Value.Date + TimePicker.Value.TimeOfDay;` for example.. you need to access the `FromDatePicker.Date` actually after looking at it you need to look at the `FromDatePicker.CustomFormat` Property.. http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.aspx – MethodMan Apr 03 '13 at 03:44
  • Hmmm no, I'll give that a shot. Also, why the downvotes? Provided a ton of detail and asked 1 exact question (why the exception) and 1 sort of vague question but still valid (is there a better way) – Hershizer33 Apr 03 '13 at 03:47
  • Not sure about the DownVote I didn't downvote.. – MethodMan Apr 03 '13 at 03:47
  • @DJKRAZE so would I put that + .TimeOfDay stuff in the constructor initializations or the value setting in the Load event? – Hershizer33 Apr 03 '13 at 03:52
  • 1
    Some people (love to) downvote without leaving any comment and often for no reason. I hate to say that: but you have to live with it. – Alina B. Apr 03 '13 at 04:03
  • @AlinaB.: There aren't so many of those people here on SO, as there are on meta, fortunately for us. – Victor Zakharov Apr 03 '13 at 15:03

2 Answers2

0

DateTime dt = DateTime.Now.Subtract(new Timespan(0, 0, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond));

Every evocation of DateTime.Now potentially is a different value. Time marches on. You need to do this:

    DateTime now = DateTime.Now;
    DateTime dt = now.Subtract(new Timespan(0, 0, now.Minute, now.Second, now.Millisecond));

Next, forget all that nonsense and use the Date property.

DateTime dt = now.Date;   // time component is set to 00:00:00
DateTime plusOne = now.AddHours(1);

With that setup it actually fails on the FromDatePicker.Value = FromDatePicker.MaxDate; line with the error message of Value of is not valid. Value should be between MinDate and MaxDate,

This may resolve the problem

Community
  • 1
  • 1
radarbob
  • 4,964
  • 2
  • 23
  • 36
0

So I achieved what I wanted through eXtreme DataBinding haha... It looks gross but works!

First, I created a property in the Form called MasterMaxDate, which will always be Now + 1 hour as described in the problem statement.

public DateTime MasterMaxDate { get; set; }

Then, I bound the ToDate picker pair's MaxDate property to MasterMaxDate:

ToDatePicker.DataBindings.Add(new Binding("MaxDate", this, "MasterMaxDate"));
ToHourPicker.DataBindings.Add(new Binding("MaxDate", this, "MasterMaxDate"));

Next, I bound the MaxDate properties of the FromDate picker pair to the Value property of the ToHour picker (as the ToHour picker is most accurate). This ensures FromDate is never > ToDate.

FromDatePicker.DataBindings.Add(new Binding("MaxDate", ToHourPicker, "Value"));
FromHourPicker.DataBindings.Add(new Binding("MaxDate", ToHourPicker, "Value"));

Next, (and now the strange part), I had to bind each picker in a pair's Value to the other to ensure they are always synced up:

FromDatePicker.DataBindings.Add(new Binding("Value", FromHourPicker, "Value"));
FromHourPicker.DataBindings.Add(new Binding("Value", FromDatePicker, "Value"));

ToDatePicker.DataBindings.Add(new Binding("Value", ToHourPicker, "Value"));
ToHourPicker.DataBindings.Add(new Binding("Value", ToDatePicker, "Value"));

Finally, set up MasterMaxDate:

DateTime now = DateTime.Now;
DateTime dt = DateTime.Now.Subtract(new TimeSpan(0, 0, now.Minute, now.Second, now.Millisecond));

MasterMaxDate = dt.AddHours(1);

And it works! It all updates live and stays in sync! (Ex: If I change the ToDate picker to a date before the FromDate picker, the FromDate picker automatically changes too to stay "behind" the ToDate picker.)

Looking back on it now it would have probably been easier to have each picker in the pair "share" a ValueChanged event and handle it that way, but I'm glad I got this to work.

UPDATE I had to implement the INotifyPropertyChanged for MasterMaxDate so it would report its new values to the DateTimePickers. Used accepted answer in this example

Community
  • 1
  • 1
Hershizer33
  • 1,206
  • 2
  • 23
  • 46