You may not always want all your application's forms in memory at once. To reduce the amount of memory required at load time, you may want to create some forms only when you need to use them. For example, a dialog box needs to be in memory only during the time a user interacts with it.
Application.CreateForm(TResultsForm, ResultsForm);
Application->CreateForm(__classid(TResultsForm), &ResultsForm);
procedure TMainForm.Button1Click(Sender: TObject); begin ResultsForm := TResultForm.Create(self); try ResultsForm.ShowModal; finally ResultsForm.Free; end; end;
void __fastcall TMainMForm::FirstButtonClick(TObject *Sender) { ResultsForm = new TResultsForm(this); ResultsForm->ShowModal(); delete ResultsForm; }
In the above example, note the use of try..finally. Putting in the line ResultsForm.Free; in the finally clause ensures that the memory for the form is freed even if the form raises an exception.
The event handler in the example deletes the form after it is closed, so the form would need to be recreated if you needed to use ResultsForm elsewhere in the application. If the form were displayed using Show you could not delete the form within the event handler because Show returns while the form is still open.
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|