June 28th, 2012, 04:20 AM
DateTimePicker MinDate problem
Hi, I need help.
I am using 2 DateTimePicker controls in my app. First is FromDate and second is ToDate control. I need when user select date in FromDate next day from that date be MinDate value of ToDate DateTimePicker to prevent user to select previous date. I prevented today mark in both controls like this:
When I execute app I get a error message. For example if FromDate selected date is 07.01.2012 and ToDate I select 08.01.2012 i get error
"Project raised exception class EDateTimeError with message 'Date is less than minimum 8.1.2012'. Process stoped."
When I click 09.01.2012 everything is fine. What am I doing wrong?
June 28th, 2012, 03:05 PM
I'm not really sure I fully understand what it is you are attempting to do, however, here are a few pointers on working with the DataTimePicker.
1) On FormCreate, you can preset the DateTimePicker dates to some reasonable date.
2) If you do not want to let the user select the current (or future) date, then the best place to catch this is in the OnChange event, where you can change it back to something reasonable.
procedure TForm1.FormCreate(Sender: TObject);
FromDate.Date := Date-7;//sets the date back 7 days
ToDate.Date := Date-1;//sets the date back 1 day
BTW: Do not attempt to display an error message with ShowMessage or MessageDlg because there is a known bug with the controls which will not allow your OnChange handler to finish correctly.
procedure TForm1.ToDateChange(Sender: TObject);
If (trunc(ToDate.Date) >= Date) then
ToDate.Date := Date-1;
if FromDate.Date >= ToDate.Date then
FromDate.Date := ToDate.Date - 1;
procedure TForm1.FromDateChange(Sender: TObject);
If trunc(FromDate.Date) >= Trunc(ToDate.Date) then
FromDate.Date := ToDate.Date-1;
Hope this helps...
Comments on this post
Last edited by majlumbo; June 29th, 2012 at 11:09 AM.
July 10th, 2012, 12:51 AM
Thanks for the reply. I bypassed system message, but now I want to do something else and getting strange result.
I want to set ToDate.Date to be 31.12 of the current year and FromDate.Date to be 01.01. of the current year.
Here is the code i wrote:
FromDate works perfectly, but I can't make ToDate to work properly. Line in red is my problem. I put last line of code to check value of Date in ToDate datetimepicker and I get that Date value is correct, showing it in form caption (31.12.2012), but in datetimepicker showing wrong date (in this case 01.01.2012).
I don't know how to solve this, I can't understand how in one datetimepicker working perfectly and in other not. I even tried to put this code in new application to test and get similar result, ToDate refuse to change value that it shows. Repaint, Refresh, Update procedures not helping.
Please people, help me with this, I am out of ideas.
July 10th, 2012, 07:02 PM
Did you mean?
July 11th, 2012, 12:31 AM
Yes, you are correct. I renamed my controlls from serbian to english and made mistake when doing that.
Originally Posted by clivew
However, in another delphi forum someone solved my problem with this code:
Thank you for attention anyway.