When we use the Delphi program, we often need to eliminate the second creation of the same form, which saves system resources to have a certain degree of beautifying the program interface. In fact, this is very simple, let's take a try!
First, we have to create a main form and name it, put a button button1 on the main form.
Then create a form, named SecondForm, and set it in menu Project | Options ... Set its creation method for Available Forms.
Switch to MainForm's unit file, select File | Use Unite ... in the menu, select SecondForm's unit file unit2 in the pop-up dialog box, then click OK.
Ok, let's write the code.
Double click on Button1 and add the following code:
If SecondForm = NIL THENBEGIN? SECONDFORM: = TsecondForm.creat (Self); ?? secondform.show;
Add the following code to the SecondForm's onclose event:
Action: = CAFREE;
Add the following code to the SecondForm's OnDestroy event:
SecondForm: = NIL;
Ok, try the program, is it a complete code: The following is the complete code:
///Unit1.pas
Unit unit1;
Interface
Uses? Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls;
TYPE? TMAINFORM = Class (TFORM) ??? Button1: tbutton; ??? Procedure Button1Click (Sender: TOBJECT); ??? {private declarations}?
Var? mainform: tMAINFORM;
IMPLEMENTATION
Uses unit2;
{$ R * .dfm}
Procedure tMainform.Button1Click (sender: TOBJECT); begin? if secondform = nil dam: = tsecondform.create (Self); ??? secondform.show ;? end;
end.//Unit2.pas///
?
Unit unit2;
Interface
Uses? Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms, Dialogs
type TSecondForm = class (TForm) ??? procedure FormClose (Sender: TObject; var Action: TCloseAction);?? ??? procedure FormDestroy (Sender: TObject) ;? private ??? {Private declarations} public ??? { PUBLIC DECLARATIONS}?
Var? SecondForm: Tsecondform;
IMPLEMENTATION
{$ R * .dfm}
Procedure Tsecondform.formClose (Sender: TpoBject; var Action: tclosection); begin? action: = cafree; end; procedure tsecondform.formDestroy (sender: TOBJECT); begin? secondform: = nil; end;
End.
/// End of the full text