procedure TfrmFormMain.btnCreateFormSecondClick(Sender: TObject); var lMyNewForm: TForm; begin lMyNewForm := prcCreateMyNewForm(TfrmFormSecond); // if not(lMyNewForm = nil) then lMyNewForm.ShowModal; end;
procedure TfrmFormMain.btnCreateFormThirdClick(Sender: TObject); var lMyNewForm: TForm; begin lMyNewForm := prcCreateMyNewForm(TfrmFormThird); // if not(lMyNewForm = nil) then lMyNewForm.ShowModal; end;
procedure TfrmFormMain.btnFormOnMemoryClick(Sender: TObject); begin prcHowFormsOnMemory; end;
procedure TfrmFormMain.FormClose(Sender: TObject; var Action: TCloseAction); begin try prcCloseAllFormsOnMemory; except on E: Exception do begin Action := TCloseAction.caNone; ShowMessage('My error:' + sLineBreak + E.Message); end; end; end;
end. ---------- Global functions ------- unit uUnitGlobalFunctions;
interface
uses System.SysUtils, Vcl.Forms, Vcl.Dialogs;
procedure prcHowFormsOnMemory; procedure prcCloseAllFormsOnMemory; function prcCreateMyNewForm(lNewFormClass: TFormClass): TForm;
var OldFormToClose: TForm;
implementation
uses uFormMain;
function prcCreateMyNewForm(lNewFormClass: TFormClass): TForm; var i: integer; begin // is not possible delete and re-create same form on screen if (not(OldFormToClose = nil)) and (OldFormToClose.ClassName = lNewFormClass.ClassName) then begin result := OldFormToClose; ShowMessage('same class is not possible delete it for while'); exit; end; // if (not(OldFormToClose = nil)) and { } (not(OldFormToClose = frmFormMain)) then begin FreeAndNil(OldFormToClose); end; // result := lNewFormClass.Create(nil); // "nil", user should "free" this object! // OldFormToClose := result; end;
procedure prcHowFormsOnMemory; var i : integer; lText: string; begin lText := ''; for i := 0 to (Screen.FormCount - 1) do lText := Screen.Forms[i].Name + sLineBreak + lText; // ShowMessage('Forms on memory:' + sLineBreak + lText); end;
procedure prcCloseAllFormsOnMemory; var i: integer; begin for i := 0 to (Screen.FormCount - 1) do begin if not(Screen.Forms[i] = frmFormMain) then Screen.Forms[i].Free; end; end;
end. ---------- FormSecond ---------- unit uFormSecond;
function prcCreateMyNewForm(lNewFormClass: TFormClass): TForm; var i : integer; lMyLabel: TLabel; begin if (not(OldFormToClose = nil)) and (OldFormToClose.ClassName = lNewFormClass.ClassName) then begin result := OldFormToClose; ShowMessage('same class is not possible delete it for while'); exit; end; // if (not(OldFormToClose = nil)) and { } (not(OldFormToClose = frmFormMain)) then begin FreeAndNil(OldFormToClose); end; // result := lNewFormClass.Create(nil); // "nil", user should "free" this object! lMyLabel := TLabel.Create(result); lMyLabel.Name := 'MyLabel1'; lMyLabel.Caption := 'Hello World'; lMyLabel.Left := result.Width - (result.Canvas.TextWidth('W') * Length(Trim(lMyLabel.Caption))); lMyLabel.Top := 20; lMyLabel.Font.Color := clWhite; // .... lMyLabel.Parent := result; // use for last! // OldFormToClose := result; end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3