2005. július 7., csütörtök

Managing a lot of Forms


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


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:

  maxForms = 100;

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

uses Unit7; // and all of the units

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

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;


uses frmControllerU;

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

  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 {... }

Nincsenek megjegyzések:

Megjegyzés küldése