Assign TForm to a variable Delphi to be used in other units

480 Views Asked by At

I have an application with three forms (one for phone, tablet and desktop) that all have the same component and all components have the same name on each form. Only one form gets created when the application starts depending on the screen solution.

I then have a unit that is called from the forms unit and I need to display the result on the calling form/unit

Right now I have to do this:

If assigned(mobileform1) do
   mobileform1.Memo1.Text := MyText
else if assigned(mobileform2) do
   mobileform1.Memo1.Text := MyText
else if assigned(desktopform) do
   mobileform1.Memo1.Text := MyText;

Is there a way to assign the Form that get created to an variable and just have one line?

CreatedForm.Memo1.Text := MyText;

I tried to assign it to a TForm Variable, but I can't use it or I don't know how.

Thanks, for any help.

1

There are 1 best solutions below

4
David Dubois On

As per the comments, the proper solution to your problem is to use a base form.

In the meantime, this should give you a quick solution to your problem:

  (CreatedForm . FindComponent ( 'Memo1' ) as tMemo)
     . Text := 'I love peanut butter sandwiches.';

Why doesn't it work to refer to CreatedForm.Memo1? Consider the following code:

type
  tMyBaseClass = class
    X : double;
  end;

  tClassA = class ( tMyBaseClass )
    Y : integer;
    Z : string;
  end;

  tClassB = class ( tMyBaseClass )
    Y : array[1..100]of integer;
    Z : string;
  end;

var
  J : tClassA;
  K : tMyBaseClass;
begin
  J := tClassA . Create;
  J . X := 3.14;
  J . Y := 4;
  J . Z := 'Hello';

  K := J;
  K . X := 2.71828;
  K . Y := 7;       // Does not compile
  K . Z := 'There'; // Does not compile

We start by defining a base class, tMyBaseClass, and we define two different class types that are derived from tMyBaseClass, tClassA and tClassB. J is declared as type tClassA. The compiler knows that J refers to an instance of a tClassA object, and therefore you can access J.X, J.Y, and J.Z. But K is of type tMyBaseClass. You can refer to K.X because it is defined in the base class, but you can't access K.Y or K.Z because those are fields of tClassA, and the compiler can't know that K is referring to an instance of tClassA.

This is essentially what's going on in your code. The base class is tForm, and you've defined class tMobileForm and tDesktopForm which are both derived from tForm. Each of those forms has a field named Memo1. You have CreatedForm which is of type tForm. You can't access CreatedForm.Memo1 because the tForm class does not have a field named Memo1.

One solution is to move the Memo1 field into a base class. So you could have

type
  tSharedForm = class ( tForm )
    Memo1 : tMemo;
  end;

  tDesktopForm = class ( tSharedForm )
    SomeOtherControl : tPanel;
  end;

  tMobileForm = class ( tSharedForm )
    SomeOtherControl : tComboBox;
  end;

If you then declare CreatedForm to be of type tSharedForm, then you can access CreatedForm.Memo1 since it is now in the base class.

This is what a base form will do for you.