Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Array of different forms


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 1 - Last Post: Jul 20, 2015 10:30 AM Last Post By: Peter Below
pss edn

Posts: 45
Registered: 1/13/04
Array of different forms  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 18, 2015 2:06 AM
Using XE7

I need to create an array of different MDI forms that are created in any order, but all forms share some common public variables and procedures.
When the array is created, I need to assign and refer the public variables or procedures in a generic way, because I do not know what kind of form is, for example item 2 of the array.
How is this done, and what is the object i need to use as the main container.

Example:

// Creating forms
arrobj[2] := TfrmFlower;
arrobj[3] := TfrmDog;
arrobj[4] := TfrmFish;

// Assigning a common public variable
arrobj[2].Name := 'Orchid';
arrobj[3].Name := 'Puppy';

// Calling a common public procedure
arrobj[1].TellName;
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: Array of different forms  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 20, 2015 10:30 AM   in response to: pss edn in response to: pss edn
pss edn wrote:

Using XE7

I need to create an array of different MDI forms that are created in
any order, but all forms share some common public variables and
procedures. When the array is created, I need to assign and refer
the public variables or procedures in a generic way, because I do not
know what kind of form is, for example item 2 of the array. How is
this done, and what is the object i need to use as the main container.

Usually you do this by creating a base form class for the MDI forms
(you never create an instance for this class, or let it be
autocreated), from which you then derive the different MDI forms
through visual form inheritance. The base class would define the shared
items, some perhaps as virtual, so they can be overridden by the
decendents as needed.

This type of design (using a base class that defines common behaviour
and descendents which specialize on that) is something you can do with
any class, of course. The only difference when it comes to designer
items, like forms, frames, or datamodules, is the way you create the
descendent: you use File --> New --> Other from the main menu to call
up the repository dialog, go to the "inheritable items" page, select
your base form class there, and click OK. The IDE then creates the new
class and unit for you, you just need to save the unit under a suitable
name and set the Name property of the form class to something sensible.


Example:

// Creating forms
arrobj[2] := TfrmFlower;
arrobj[3] := TfrmDog;
arrobj[4] := TfrmFish;

If you follow the common naming standard that would store the form
classes into the array, not create form instances. Anyway, you do not
need a separate array for your scheme. You can create MDI child forms
in code or let them be autocreated the normal way. There already is a
list of the created MDI child forms you can use: the main form's
MDIChildren property. You can iterate over that and check whether a MDI
child you get from it is derived from your base class or not. If it is
you cast the reference from the array to the base class and access the
common method or property that was declared public in the base class
directly:

for i:= 0 to application.mainform.mdichildcount-1 do
if application.mainform.MdiChildren[I] is TMyMDIBaseformClass then
TMyMDIBaseformClass(application.mainform.MdiChildren[I]).TellName;


--
Peter Below (TeamB)

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02