others - delphi 从TButton调用事件

我试图从表单的"OnCloseQuery"调用事件TButton。

下面是我的OnCloseQuery的代码:


procedure TfrmAllowance.FormCloseQuery(Sender: TObject; var CanClose: Boolean);


begin


 // prompt close query and saving of data during parent and child closing


 with dm_u.dmPayroll do


 begin


 if MessageDlg('Are you sure you want to close Setup Allowance?',


 mtConfirmation, [mbYes, mbCancel], 0) <> mrYes then


 begin


 CanClose := False;


 end


 else if cdsAllowance.State in [dsEdit, dsInsert{, dsBrowse}] then


 begin


 manpower_u.frmManpower.btnAllowances.Enabled := true;


 if MessageDlg('Do you wish to save the Setup Allowance changes?',


 mtConfirmation, [mbYes, mbNo], 0) = mrYes then


 begin


 cdsAllowance.ApplyUpdates(0);


 if (cdsAllowance.ApplyUpdates(-1) = 0) then


 begin


 cdsAllowance.Refresh;


 end;


 end;


 end;


 cdsAllowance.Active := False;


 end;


end;




FormCloseQuery(?????????);



时间:

如果要调用它,请使用类似下面的内容:


procedure TForm1.Button1Click(Sender: TObject);


var


 CanClose: boolean;


begin


 CanClose := True;


 FormCloseQuery(Self, CanClose);


 if CanClose then


 // whatever


end;




procedure TForm1.Button1Click(Sender: TObject);


begin


 Close;


end;



下面是处理这种情况的简单方法,它适用于事件,而不仅仅是FormCloseQuery事件。

FormCloseQuery中的所有代码移动到MyFormCloseQuery并将它作为调用FormCloseQuery中的。

换句话说,将代码更改为


procedure TfrmAllowance.MyFormCloseQuery(Sender: TObject;


begin


 // prompt close query and saving of data during parent and child closing


 with dm_u.dmPayroll do


 begin


 if MessageDlg('Are you sure you want to close Setup Allowance?',


 mtConfirmation, [mbYes, mbCancel], 0) <> mrYes then


 begin


 CanClose := False;


 end


 else if cdsAllowance.State in [dsEdit, dsInsert{, dsBrowse}] then


 begin


 manpower_u.frmManpower.btnAllowances.Enabled := true;


 if MessageDlg('Do you wish to save the Setup Allowance changes?',


 mtConfirmation, [mbYes, mbNo], 0) = mrYes then


 begin


 cdsAllowance.ApplyUpdates(0);


 if (cdsAllowance.ApplyUpdates(-1) = 0) then


 begin


 cdsAllowance.Refresh;


 end;


 end;


 end;


 cdsAllowance.Active := False;


 end;


end;



procedure TfrmAllowance.FormCloseQuery(Sender: TObject; var CanClose: Boolean);


begin


 // Maybe precede this code by logic which checks whether the following should execute


 MyFormCloseQuery(Sender;


 CanClose := True;


end;



在你的情况下,你可以


procedure TfrmAllowance.Button1Click(Sender: TObject);


begin


 MyCloseQuery(Sender);


end;



...