Search code examples
formsdelphidelphi-7

delphi 7 closing available modal form Onactivate under certain condition


im working on a project using delphi 7, The project is a maintenance project and im not the original coder of the project, i have a situation where i need to close a available form after it had been created through code under certain situations,The form is model

here is sample code of that

       var 
       frmStratum : TfrmStratum;
       begin
          if not assigned(frmStratum) then myMainForm.OnExecute(PropAction);
        end; 

inside myMainForm.OnExecute(PropAction); i have

frmStratum := TfrmStratum.Create(Self, Self as IStratum,inttostr(m_surveyno),Module,m_stations,false);

now the procedure TfrmStratum.FormActivate of TfrmStratum i do lots of calucaltion and write to database

var
  if (bMassStratumExport) AND (bDoneOne)  then
  begin
             //write to database..
  end;

now i have to do this atleast 20 times that is

1. Create the form
2. onactivate do database writing
3. close TfrmStratum

since it is a modal form i cannot close if below from where i create it,so i wanted to close it onactivate as soon as the step 2 is done

now i have tried this

  if (bMassStratumExport) AND (bDoneOne) AND NOT (bReadyToclose) then
 begin
    //do database writing
    if bNowClo then frmStratum.close;
 end

EDIT :(edited to make the question more clear)

Onactivate of the form(frmStratum) , i want to close the modal form (frmStratum),so i do this

procedure TfrmStratum.FormActivate(Sender: TObject);
 begin
  if (bMassStratumExport) AND (bDoneOne) AND NOT (bReadyToclose) then
    begin
      //do database writing
      if bNowClo then self.close;// i need to close the form after after doing database write
    end
  end;

but the control while bugging goes to self.close but it doesnt close the form. how to tackle this ?


Solution

  • In the past when I needed to close a form during activation I posted a message to myself instead of calling self.close.

    PostMessage(Self.Handle, WM_CLOSE, 0, 0);
    

    I tried to find my original source that pointed me in this direction but I could not find it.

    PostMessage will return immediately and not wait for the message to be processed. Once the OnActivate function is finished and the message Delphi processing loop processes the message close will be called on your form.