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
This is a
known issuein 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 eitherDTM_GETSYSTEMTIMEmessage, or theDateTime_GetSystemtimemacro, which internally sends this message. If the message (or the macro) returnsGDT_VALIDvalue, and theDTS_SHOWNONEstyle is used (in Delphi whenShowCheckboxproperty 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:
So, you can make a helper function like this to workaround the wrong Delphi implementation: