Right way for datepickers validaton in c #? (window shapes)

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; //setting datepicker value datepickerFrom.Value = new DateTime(year, month, day, hour, minute, 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

+5
source share
2 answers

Solution 1:

You can handle the datePickerTo close event and do something like:

 private void dateTimePickerTo_CloseUp(object sender, EventArgs e) { DateTime fromdate = Convert.ToDateTime(dateTimePickerFrom.Value); DateTime todate1 = Convert.ToDateTime(dateTimePickerTo.Value); if (fromdate > todate1) //Error } 

You can also use DateTime.Compare , which will get two dates as

 int result = DateTime.Compar(dateTimePickerFrom.Value ,dateTimePickerTo.Value); 

if result 1 means From date earlier, see this link .

Note1:

but, as you said, if the user enters From or To text fields, then the closeup event never fires, so you need to compare them where you want to process such as clicking a button.

Note2:

Like the @Sinatr comment, if the value is DateTime , then you don't need to convert it, so the code will look like this:

  if (dateTimePickerFrom.Value >dateTimePickerTo.Value) //Error 
+3
source

Your suggestion will lead to a terrible interface. Suppose the following case:

 From = 1 jan 2000 To = 1 feb 2000 

The user wants to change both values ​​until 2010. It starts with a value from:

 From = 1 jan 2010 

Now he wants to change the TO value to February 1, 2010. Alas, he cannot.

Proper use: add some button with which the operator can confirm that he has changed all the data, will begin checking and updating. On Windows, this button is usually called Apply Now or OK . Why deviate from this Windows standard.

 private void OnFormLoading(object sender, ...) { this.FromDate.MinValue = ... // use the real absolute min value you want ever to allow this.FromDate.MaxValue = ...; this.ToDate.MinValue = ...; this.ToDate.MaxValue = ...; } 

Do not check if the operator makes a change. Strat checks the input values ​​when it indicates that it has finished making changes:

 private void OnButtonApplyNow_Clicked(object sender, ...) { bool InputOk = CheckInput(); if (!inputOk) { ShowIncorrectInput(); // for instance using a MessageBox } } 
+2
source

Source: https://habr.com/ru/post/1274611/


All Articles