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.
- The From pickers MaxDates can never be more than the To pickers MaxDates
- 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