Mirror

Managing a lot of Forms (Views: 702)


Problem/Question/Abstract:

How do you controll hundreds of forms by number in a main unit ?

Answer:

Suppose you have a call from a dear developer that had about 100 forms that he have to create and run. So you tell him, create and run it by number in, here's a solution to this problem...

The following part is to be placed on the main form's unit, maybe a frmController unit and the purpose is to have a couple of arrays that are referenced by number and then accessible to manipulate:

const
  maxForms = 100;

var
  frmController: TForm1
  frmArray: array[1..maxForms] of TForm;
  frmRefArray: array[1..maxForms] of TFormClass;

implementation
uses Unit7; // and all of the units

procedure TForm1.btnfrmController(sender: TObject);
begin
  ... // iterating or indexing as you like
  frmArray[7] := frmRefArray[7].create(self);
  frmArray[7].showModal; // whatever you need
  frmArray[7].free;
end;

The next step is, each form must register itself in the array of the controller or main form unit. This can be done at load time or runtime, let's get straight to the implementation part:

unit Unit7;

implementation

uses frmControllerU;

procedure TForm7.FormCreate(sender: TObject);
frmArray[7] := self;
end;

initialization
  frmRefArray[7] := TForm7 //hard coded

The last part means that you must tell the array which class has to be associated with which array element. So you get by ObjectPascal the classReference, another way that this can be done is by using an extra unit where all of the form's information is centralized and easier to maintain, but the idea remains the same.

ps: If the form is already instantiated, then you find it through the TScreen object (don't scream use TScreen ;):

for i := 0 to screen.FormCount - 1 do
  if screen.forms[i].className = 'TForm7' then {... }

<< Back to main page