I would think that itβs better not even to create the form in the first place. IF you execute some logic that determines that the form is not even needed, and this logic contains the state that is important for the form, then refactify the logic into a separate object (or even a data module) and transfer the object to the form as property. Here is a simple example (using the object approach):
UNIT1
type TOFormTests = class fStateData : string; public function IsForm1Needed( someparam : string) : boolean; property StateData : string read fStateData write fStateData; end;
UNIT2
uses : UNIT1; type TForm1 = class(tForm) : procedure SetFormTests(value : tOFormTests); property FormTests : TOFormTests read fFormTests write SetFormTests; end; procedure SetFormTest(Value:TOFOrmTests); begin fFormTests := Value; // perform gui setup logic here. end;
then somewhere in your code where you want to determine whether to show your gui or not use something like the following:
var Tests : TOFormTests; begin tests := tOFormTests.create; try if Tests.IsForm1Needed('state data goes here') then begin Form1 := tForm1.create(nil); try Form1.FormTests := Tests; if Form1.ShowModal = mrOk then // handle any save state logic here. ; finally FreeAndNil(Form1); end; end; finally freeAndNil(Tests); end; end;
This also assumes that the form is NOT on the auto-create list and should be shown modal.
source share