It just won't work with a datepicker check. I have a datepicker From and datepicker to , so I want the user to not perform some kung fu and set datepicker From more to datepicker Q , I ran into some questions but couldnβt find the answer, so I tried to make the easiest way about which I could think of:
Set the MaxDate property for datepicker from in the form_load event
private void Form1_Load(object sender, EventArgs e) { datepickerFrom.MaxDate = datepickerFrom.Value; }
Then do the same for the value_changed event
private void datepickerFrom_ValueChanged(object sender, EventArgs e) { datepickerFrom.MaxDate = datepickerFrom.Value; }
It was easy and simple, just a few lines of code, and I only needed the datepickerFrom_ValueChanged event, but lately I tried to enter a date in datepicker to select it, and then all the hell broke. So I came up with some solution for checking, instead of setting the MaxDate property, I tried this.
private void dtFrom_ValueChanged(object sender, EventArgs e) { DateTime from = datepickerFrom.Value; DateTime to = datepickerTo.Value; int year= from.Year > to.Year ? to.Year : from.Year; int month = from.Month > to.Month ? to.Month : from.Month; int day = from.Day > to.Day ? to.Day : from.Day; int hour = from.Hour > to.Hour ? to.Hour : from.Hour; int minute = from.Minute > to.Minute ? to.Minute : from.Minute; int second = from.Second > to.Second ? to.Second : from.Second;
This works great, but it feels a bit of a headache, and I have to do it for the datepickerTO_ValueChanged event, of course, I could do one method and call it twice, but still feel like there is a dough for this, so any suggestions ?
thank you for your time