Juke
Juke

Reputation: 135

Call an Event from a TButton

I am trying to call an Event "OnCloseQuery" from a TButton in the same Form.

Here's the code of my 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;

And here's the code of my TButton to call the above procedure and I do not know what to put in the parameters:

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

I am not so familiar with calling an event but a quick clue would help me figure out.

Upvotes: 0

Views: 387

Answers (1)

pepak
pepak

Reputation: 742

There is very little point in calling an OnCloseQuery event because it doesn't really do much. But if you really do want to call it, then use something like this:

procedure TForm1.Button1Click(Sender: TObject);
var
  CanClose: boolean;
begin
  CanClose := True;
  FormCloseQuery(Self, CanClose);
  if CanClose then
    // whatever
end;

But I rather suspect what you actually want to do is this:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Close;
end;

That will call the OnCloseQuery indirectly AND it will perform all the other handling needed for closing a form.

Upvotes: 4

Related Questions