Check DBDateTimePicker

i downloaded DBDateTimePicker, which I use to edit dates in the table, start date and end date. When adding to a table in regular editing blocks, I use the following code before adding and submitting, to prevent the completion date set before the start date.

if  (DTPAddStartDate.Date) > (DTPAddCompletionDate.Date) then
begin

      raise Exception.Create ('The Completion date of a Job can not be before a Start Date');
      abort;
end;

The problem is that I want to be able to achieve the same when you edit dates via DBDateTimePickers, if I have similar code in the BeforePost event or something, then I will get premature error messages, as the user may not have the ability to edit another related DBDateTimePicker. I have no idea how I can relate these two fields so that I can somehow justify them or come up with another solution to the problem, any suggestions? (I use adotable, connected to access, and the component was downloaded from the following link: http://apollosoft.net/jms/ ).

+3
source share
2 answers

, TDBDateTimePicker this page, , .

OnValidate , , , , , - .

, , (?) DB , , - , - OnCanUpdate .

- , , , TDBDateTimePicker. DBDateTimePicker.pas , , OnCanUpdate Object Inspector. , , Allowed, True, , False ( ), . , , , .

type
  TDBDateTimePicker = class;
  TOnCanUpdate = procedure(Sender: TDBDateTimePicker; 
    var Allowed: Boolean) of object;
  TDBDateTimePicker = class(TDateTimePicker)
  private
    FOnCanUpdate: TOnCanUpdate;
    procedure CMExit(var Message: TCMExit); message CM_EXIT;
  published
    property OnCanUpdate: TOnCanUpdate read FOnCanUpdate write FOnCanUpdate;
  end;

procedure TDBDateTimePicker.CMExit(var Message: TCMExit);
var
  Allowed: Boolean;
begin      
  if Assigned(FOnCanUpdate) then
  begin
    Allowed := False;
    FOnCanUpdate(Self, Allowed);    
    if not Allowed then
    begin
      SetFocused(True);
      Exit;
    end;
  end;

  try
    FDataLink.UpdateRecord;
  except
    SetFocus;
    raise;
  end;
  SetFocused(False);
  inherited;
end;

, OnCanUpdate ( , ).

procedure TForm1.DTPAddStartDateCanUpdate(Sender: TDBDateTimePicker; 
  var Allowed: Boolean);
begin
  if (DTPAddStartDate.Date) > (DTPAddCompletionDate.Date) then
  begin
    // the Allowed parameter is in current code initialized to False
    // when it comes into this event, so the following line has no
    // sense here
    Allowed := False;
    // here you can display the error message or raise exception or just
    // whatever you want, the record won't be modified in any way
    Application.MessageBox('The completion date of a job cannot be before a ' +
      'start date. The record won''t be modified ;-)', 'Date Input Error...',
      MB_YESNO + MB_ICONSTOP + MB_TOPMOST);
  end
  else
    // only setting the Allowed to True will actually allows the record to be
    // updated, so if your validation passes, set the Allowed to True
    Allowed := True;
end;
+4

Caviate: , . , - . , , ( , ), , .

//Pseudo Code--I'm sure it is using a standard event model
myForm.DTPAddCompletionDate.OnChange(...)
begin
  //do your check if it is successful something like... 
  if not (DTPAddStartDate.Date > DTPAddCompletionDate.Date) then
    return;
  else
    someSubmitButton.enabled = true;
end;

Delphi , , , , . . .

0

All Articles