j.kaspar
j.kaspar

Reputation: 761

Delphi TAdvDateTimePicker blank default value

I have a form with two TAdvDateTimePicker components, and few other edits (in delphi XE7). Both datetime fields are optional, so if the user don't want to fill them, in the database will both be null.

Problem is, that unlike for example good old TDateEdit, the TAdvDateTimePicker does not accept a blank value even as default. I know, that something like "null datetime" does not exist, but I need to know, if user did or did not want to fill those dates. So leaving today's date as default, and using onChange, onEnter or whatever action to check if a value has been set, isn't a solution - he may want to use today's date, so he leaves it like that.

There is of course a possibility to set the default value to, let's say 1.1.1900 (an obvious nonsense) and then check if it has changed, but that is the last thing I would like to do. Is there a better way?

Thanks

Upvotes: 2

Views: 2058

Answers (1)

j.kaspar
j.kaspar

Reputation: 761

With thanks to Sertac Akyuz, here is the simplest solution:

onActivate of the form, set .Format property to a space (' '):

procedure TfrmDeliveriesEdit.FormActivate(Sender: TObject);
begin
  DateTimePicker1.Format:= ' ';
end;

Then, onEnter of the picker, set the format to empty string ('') to revert back, if the user wants to fill a real value

procedure TfrmDeliveriesEdit.DateTimePicker1Enter(Sender: TObject);
begin
  DateTimePicker1.Format:= '';
end;

Edit: to return back to "null" value, check delete key in onKeyDown event like this:

procedure TfrmDeliveriesEdit.dpTaxDateKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_DELETE then dpTaxDate.Format:= ' ';
end;

Upvotes: 2

Related Questions