Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 06-28-2012, 08:24 AM
kdalibor kdalibor is offline
Junior Member
 
Join Date: Jun 2012
Posts: 17
Default datetimepicker mindate problem

Hi, I need help again.

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:

Quote:
procedure TfrmGlavna.FromDateDropDown(Sender: TObject);
var
wnd: HWND;
style: Integer;
begin
wnd:= DateTime_GetMonthCal(dtpOd.Handle);
if wnd <> 0 then
begin
style := GetWindowLong(wnd, GWL_STYLE) ;
SetWindowLong(wnd, GWL_STYLE, style or MCS_NOTODAY or MCS_NOTODAYCIRCLE) ;
end;
end;
Then this:

Quote:
procedure TfrmGlavna.FromDateExit(Sender: TObject);
begin
ToDate.MinDate:= FromDate.Date + 1;
end;
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?
  #2  
Old 06-28-2012, 02:01 PM
OldTurtle OldTurtle is offline
Junior Member
 
Join Date: Apr 2010
Posts: 18
Default

It seems that there are some bugs in TDateTimePicker routines.

A way to bypass this is to avoid the default error checks of component:

In OnChange of FromDate event:
procedure TForm1.FromDateChange(Sender: TObject);
begin
ToDate.Date:= FromDate.Date + 1;
end;

In OnCloseUp event of ToDate:

procedure TForm1.ToDateCloseUp(Sender: TObject);
begin
if DateToStr(ToDate.Date) <= DateToStr(FromDate.Date) then
ShowMessage('wrong date');
end;

This bypass the system error messages. DateToStr is necessary because seems that the component use also the Time part.

Good luck.
  #3  
Old 07-10-2012, 05:46 AM
kdalibor kdalibor is offline
Junior Member
 
Join Date: Jun 2012
Posts: 17
Default

Thanks for the reply. I bypassed system message with ShowMessage, 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:

Quote:
procedure TMainForm.FormCreate(Sender: TObject);
var
D, M, G: Word;
begin
ShortDateFormat:= 'dd.mm.yyyy';
DecodeDate(Date, G, M, D);
grdTabela.Cells[0,0]:= 'Group';
grdTabela.Cells[1,0]:= 'Zone';
grdTabela.Cells[2,0]:= 'Full tax amount';
grdTabela.Cells[3,0]:= 'Tax reduction';
grdTabela.Cells[4,0]:= 'Period';
grdTabela.Cells[5,0]:= 'Whole year';
grdTabela.Cells[6,0]:= 'Calculated';
grdTabela.Selection := TGridRect(Rect(-1,-1,-1,-1));
FromDate.MinDate:= Date - DayOfTheYear(Date) + 1;
FromDate.MaxDate:= Date + DaysInAYear(G) - DayOfTheYear(Date);
FromDate.Date:= FromDate.MinDate;
ToDate.MinDate:= FromDate.MinDate;
ToDate.MaxDate:= FromDate.MaxDate;
FromDate.Date:= FromDate.MaxDate;
MainForm.Caption:= 'Tax for the ' + IntToStr(G) + '. year, control value: ' + DateToStr(ToDate.Date);
end;
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.

  #4  
Old 07-10-2012, 08:32 PM
OldTurtle OldTurtle is offline
Junior Member
 
Join Date: Apr 2010
Posts: 18
Default

Try to change your code this way:

procedure TMainForm.FormCreate(Sender: TObject);
var
D, M, G: Word;
begin
ShortDateFormat:= 'dd.mm.yyyy';
DecodeDate(Date, G, M, D);
grdTabela.Cells[0,0]:= 'Group';
grdTabela.Cells[1,0]:= 'Zone';
grdTabela.Cells[2,0]:= 'Full tax amount';
grdTabela.Cells[3,0]:= 'Tax reduction';
grdTabela.Cells[4,0]:= 'Period';
grdTabela.Cells[5,0]:= 'Whole year';
grdTabela.Cells[6,0]:= 'Calculated';
grdTabela.Selection := TGridRect(Rect(-1,-1,-1,-1));
FromDate.MinDate:= Date - DayOfTheYear(Date) + 1;
FromDate.MaxDate:= Date + DaysInAYear(G) - DayOfTheYear(Date);
FromDate.Date:= FromDate.MinDate;
ToDate.MinDate:= FromDate.MinDate;

//changed
ToDate.MaxDate:= FromDate.MaxDate + 1;
ToDate.Date:= FromDate.Date + DaysInAYear(G) - 1;
//end
MainForm.Caption:= 'Tax for the ' + IntToStr(G) + '. year, control value: ' + DateToStr(ToDate.Date);
end;


If you have the same value for Date and MaxDate, the DateTimerPicker raise an exception (ERROR), so you must set the ToDate.MaxDate to 01/01/ year+1
  #5  
Old 07-11-2012, 05:21 AM
kdalibor kdalibor is offline
Junior Member
 
Join Date: Jun 2012
Posts: 17
Default

That works perfectly. Thank you a lot.
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 03:21 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.