Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does TDateTimePicker.Checked return always True on Windows 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

like image 311
Re0sless Avatar asked Oct 16 '12 13:10

Re0sless


1 Answers

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;
like image 65
TLama Avatar answered Nov 03 '22 17:11

TLama