7

I have an application, built in Delphi 2007, with a TDateTimePicker on the form. This date time picker has the ShowCheckbox property set to True, which next to date or time displays a check box, that is automatically selected whenever a date is picked by user, or when the date or time is changed by code. The state of this check box can also be manually controlled by the user and its state can be determined by the Checked property.

The following code shows how to determine the state of this check box in the OnChange event:

procedure TForm1.FormCreate(Sender: TObject);
begin
  DateTimePicker1.ShowCheckbox := True;
end;

procedure TForm1.DateTimePicker1Change(Sender: TObject);
begin
  ShowMessage('Checked: ' + BoolToStr(DateTimePicker1.Checked, True));
end;

The above code works as expected on Windows XP, but on Windows 7, the Checked property returns always True regardless of the real state of that check box.

Why does Checked property return always True, even when the check box is unchecked ? Is there a way to fix or workaround this somehow ?

P.S. My application uses Windows themes

TLama
  • 75,147
  • 17
  • 214
  • 392
Re0sless
  • 10,678
  • 6
  • 51
  • 66

1 Answers1

9

This is a known issue in Delphi date time picker control's implementation (fixed in Delphi 2009, as @Remy pointed in his comment). To query the state of a date time picker check box should be used either DTM_GETSYSTEMTIME message, or the DateTime_GetSystemtime macro, which internally sends this message. If the message (or the macro) returns GDT_VALID value, and the DTS_SHOWNONE style is used (in Delphi when ShowCheckbox property is True), it indicates that the control's check box is checked and that control contains a valid date time.

Here's the example of how to use the mentioned macro to determine the check box state:

uses
  CommCtrl;

procedure TForm1.DateTimePicker1Change(Sender: TObject);
var
  SysTime: SYSTEMTIME;
begin
  if DateTime_GetSystemTime(DateTimePicker1.Handle, @SysTime) = GDT_VALID then
    ShowMessage('Check box is checked!')
  else
    ShowMessage('Check box is not checked!');
end;

So, you can make a helper function like this to workaround the wrong Delphi implementation:

uses
  CommCtrl;

function IsDateTimePickerChecked(ADateTimePicker: TDateTimePicker): Boolean;
var
  SysTime: SYSTEMTIME;
begin
  Result := DateTime_GetSystemTime(ADateTimePicker.Handle, @SysTime) = GDT_VALID;
end;

procedure TMyForm.ButtonOneClick(Sender: TObject);
begin
  if IsDateTimePickerChecked(DateTimePicker1) then
    ShowMessage('Check box is checked!')
  else
    ShowMessage('Check box is not checked!');
end;
TLama
  • 75,147
  • 17
  • 214
  • 392
  • 2
    The "known issue" was fixed in D2009. – Remy Lebeau Oct 16 '12 at 19:30
  • Are there any OS issues with this workaround, or does it work the same for XP, VISTA, and 7? – mwhobrey Oct 16 '13 at 14:30
  • 1
    @bullrider, this Delphi implementation issue I met since Windows 7, but it may well be since Windows Vista (but I don't know that for sure). In Windows XP it didn't happen. In all cases, you'll better do if you implement any sort of fix for this issue and make your application work correctly for newer Windows versions. – TLama Oct 16 '13 at 14:38
  • @TLama Right, I know that the problem is most prevalent in Windows 7, but as far as the workaround goes, does it have any issues in the older versions of Windows? I have a program that is used on the various version of Windows, from XP on up, and I was wondering if this workaround will cause any issues in the older versions? – mwhobrey Oct 16 '13 at 14:52
  • 1
    @bullrider, ah, I see what you mean. No, it won't cause anything wrong. Such code will work also on older versions of Windows. That message (and macro) are available since Windows 2000. – TLama Oct 16 '13 at 14:59