Delphi Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreDelphi Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 28th, 2012, 04:20 AM
kdalibor kdalibor is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 3 kdalibor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 37 sec
Reputation Power: 0
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:

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?

Reply With Quote
  #2  
Old June 28th, 2012, 03:05 PM
majlumbo majlumbo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 253 majlumbo User rank is Lance Corporal (50 - 100 Reputation Level)majlumbo User rank is Lance Corporal (50 - 100 Reputation Level)majlumbo User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 2 Days 23 h 37 m 14 sec
Reputation Power: 5
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.

Code:
procedure TForm1.FormCreate(Sender: TObject);
begin
   ...
   FromDate.Date := Date-7;//sets the date back 7 days
   ToDate.Date := Date-1;//sets the date back 1 day
end;


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.

Code:
procedure TForm1.ToDateChange(Sender: TObject);
begin
   If (trunc(ToDate.Date) >= Date) then
   begin
      ToDate.Date := Date-1;
      if FromDate.Date >= ToDate.Date then
         FromDate.Date := ToDate.Date - 1;
   end;
end;

procedure TForm1.FromDateChange(Sender: TObject);
begin
   If trunc(FromDate.Date) >= Trunc(ToDate.Date) then
   begin
      FromDate.Date := ToDate.Date-1;
   end;
end;


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.

Hope this helps...
Comments on this post
kdalibor agrees!

Last edited by majlumbo : June 29th, 2012 at 11:09 AM.

Reply With Quote
  #3  
Old July 10th, 2012, 12:51 AM
kdalibor kdalibor is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 3 kdalibor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 37 sec
Reputation Power: 0
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:

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.

Reply With Quote
  #4  
Old July 10th, 2012, 07:02 PM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
Quote:
FromDate.Date:= FromDate.MaxDate;

Did you mean?
Code:
ToDate.Date:= FromDate.MaxDate;

Reply With Quote
  #5  
Old July 11th, 2012, 12:31 AM
kdalibor kdalibor is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 3 kdalibor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 37 sec
Reputation Power: 0
Quote:
Originally Posted by clivew
Did you mean?
Code:
ToDate.Date:= FromDate.MaxDate;


Yes, you are correct. I renamed my controlls from serbian to english and made mistake when doing that.

However, in another delphi forum someone solved my problem with this code:

Quote:
ToDate.MaxDate:= FromDate.MaxDate + 1;
ToDate.Date:= FromDate.Date + DaysInAYear(G) - 1;


Thank you for attention anyway.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreDelphi Programming > DateTimePicker MinDate problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap