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?
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.
CodePudding user response:
Internally the control (TDatePicker
) uses the Date
property to store the Value.
Before you change the value on component you can load this value and store it.
At the end you can see if there are changes in the Date
property, with respect to the initial value.
If there are no changes, it may be due to:
- Change has been canceled.
- The same date has been selected.
In both cases there is no need to update the TStringGrid
.
Test something like this:
...
private
oldValue:tDate;
...
procedure TForm3.DatePicker1Click(Sender: TObject);
begin
oldDate := DatePicker1.Date;
end;
procedure TForm3.DateTimePicker1CloseUp(Sender: TObject);
begin
if DatePicker1.Date <> OldDate then
StringGrid1.Cells[col, row] := FormatDateTime('dd/mm/yyyy', DatePicker1.Date);
end;
CodePudding user response:
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.