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.
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:
Why doesn't it work to refer to
CreatedForm.Memo1? Consider the following code:We start by defining a base class,
tMyBaseClass, and we define two different class types that are derived fromtMyBaseClass,tClassAandtClassB.Jis declared as typetClassA. The compiler knows thatJrefers to an instance of atClassAobject, and therefore you can accessJ.X,J.Y, andJ.Z. ButKis of typetMyBaseClass. You can refer toK.Xbecause it is defined in the base class, but you can't accessK.YorK.Zbecause those are fields oftClassA, and the compiler can't know thatKis referring to an instance oftClassA.This is essentially what's going on in your code. The base class is
tForm, and you've defined classtMobileFormandtDesktopFormwhich are both derived fromtForm. Each of those forms has a field namedMemo1. You haveCreatedFormwhich is of typetForm. You can't accessCreatedForm.Memo1because thetFormclass does not have a field namedMemo1.One solution is to move the
Memo1field into a base class. So you could haveIf you then declare
CreatedFormto be of typetSharedForm, then you can accessCreatedForm.Memo1since it is now in the base class.This is what a base form will do for you.