delphidatepickervclcancel-button

In the TDatePicker.OnCloseUp event, how to determine if the user clicked Cancel?


I have a perhaps simple question:

I have a TStringGrid in Delphi 10.4, and a TDatePicker there, with which I want to select a date in the FixedCol. When I click on the header cell, the TDatePicker opens and also shows today's date. I've pre-selected today's date in the TForm.OnCreate event for the TDatePicker. Furthermore, I have an OnCloseUp event for the TDatePicker, which writes the selected date into the cell.

However, it does this both when I confirm with the tick and when I cancel with the X.

How can I query whether the user has clicked Cancel so that I don't then enter a value?

DatePicker

My code is more or less like this:

procedure TForm1.DatePicker1CloseUp(Sender: TObject);
begin
  //if DatePicker.Cancel ?? then
  //   exit;
  StringGrid1.Cells[col, row] := FormatDateTime(dateformat, DatePicker1.Date);
end;

I only found, that if I select a date and click OK, I get the selected date. Selecting another date and clicking Cancel returns the predefined date.


Solution

  • After trying I found a solution at least for Mouse Input:

    GetCursorPos(MausPos);
    MausPos := DatePicker1.ScreenToClient(MausPos);
    if (MausPos.X > DatePicker1.Width / 2)  then
      ShowMessage('Cancel')
    else
      ShowMessage('OK');
    

    I take the Cursor position relative to the TDatePicker. If the X-Coordinate is on the right half, the cancel Button is pressed and I can exit the function without writing the date to the StringGrid.