Kromster
Kromster

Reputation: 7397

How to ensure date entered into TcxDateEdit is within MinDate/MaxDate?

TcxDateEdit allows to set properties for MinDate and MaxDate. However I can't find a key that would make the control to validate the entered date automatically and ensure that the date is within the specified range.

For example:

MinDate := EncodeDate(1900, 1, 1);
MaxDate := EncodeDate(2100, 1, 1);

When I enter a date 1.1.1111 it throws an error instead of fitting it into the range 01.01.1900 .. 01.01.2100

Upvotes: 2

Views: 2411

Answers (1)

Kromster
Kromster

Reputation: 7397

Following code in Properties.OnValidate does the job:

procedure TForm.deDatePropertiesValidate(Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
var
  ed: TcxDateEdit; //So we can use single handler for different controls
  dt: TDateTime;
begin
  ed := TcxDateEdit(Sender); 
  dt := StrToDateDef(DisplayValue, ed.Properties.MinDate);

  if not InRange(dt, ed.Properties.MinDate, ed.Properties.MaxDate) then
  begin
    DisplayValue := EnsureRange(dt, ed.Properties.MinDate, ed.Properties.MaxDate);
    ed.EditValue := DisplayValue;
  end;

  Error := False;
  ErrorText := '';
end;

Upvotes: 2

Related Questions