1

I have a DateTimePicker on a Delphi 6 form with a default date of 30/12/1899. I want users to be able to click on it or open the dropdown calendar and it select the current date. Using the OnClick procedure with:

DateTimePicker.Date := Date

sets the date in the editable part to Date when the users click on it or the calendar dropdown button but will not force the calendar to automatically select today's date. The result is the same if I use this code in the DateTimePicker's OnDropDown procedure.

Do I need to use something like in this post to manipulate the calendar? Or is there a simple property that I've missed?

Thanks Matt

Community
  • 1
  • 1
Matthew
  • 164
  • 1
  • 2
  • 10
  • Why should users have to *select* the current date? Why not just have it set to the current date from the start? Then users only have to interact with it if they don't already like the default setting. Furthermore, won't your plan be annoying if users ever change their minds, or make mistakes? If they choose the wrong date the first time, then the second time they click the button, their choices will be reset to the current date instead of acting like a normal date-time picker and staying at the current value. – Rob Kennedy Aug 18 '14 at 14:55

2 Answers2

7

You can update the month calendar window directly via MonthCal_SetCurSel.
Something like this (I leave the "default" logic up to you):

uses Commctrl;

type TDateTimePickerAccess = class(TDateTimePicker);

procedure TForm1.DateTimePicker1DropDown(Sender: TObject);
var
  ST: TSystemTime;
  CalendarHandle: HWND;
begin
  DateTimePicker1.Date := Date;
  DateTimeToSystemTime(Date, ST);
  CalendarHandle := TDateTimePickerAccess(DateTimePicker1).GetCalendarHandle;
  MonthCal_SetCurSel(CalendarHandle, ST);
end;

Personally I would set the default date to whatever the default date is (Date).

kobik
  • 21,001
  • 4
  • 61
  • 121
0

I cannot find an existing property that will solve your request. Looks like the link you provided solves the problem but I have not tested.

A simple "Hacky" solution could be as follows.

procedure TFormMain.FormCreate(Sender: TObject);
var  DefaultDate : TDate;
begin
  //Set the default date
  DefaultDate := EncodeDate(1899, 12, 30);
  DateTimePicker1.MinDate := DefaultDate; //Use MinDate to store the default date
  DateTimePicker1.Date := DefaultDate;
end;

procedure TFormMain.DateTimePicker1DropDown(Sender: TObject);
begin
  //Only continue if the component is set to the default date
  if CompareDate(DateTimePicker1.MinDate, DateTimePicker1.Date) <> 0 then exit;

  //Hack: Change the DateTimePicker's Kind Type to disrupt the current drop down event
  DateTimePicker1.Kind := dtkTime;
  DateTimePicker1.Kind := dtkDate;

  //Change to today
  DateTimePicker1.DateTime := now;

  //Send a message to the drop down the calander once again
  SendMessage(DateTimePicker1.Handle,WM_SYSKEYDOWN,VK_DOWN, 0);
end;
Lars
  • 1,428
  • 1
  • 13
  • 22