Is it possible to tell Unit1 Form1 to create another self ,
Application.CreateForm(TForm1, Form1);
and trough the first form1 to be able to tell the difference between the original form1 components and the second, new form1 components.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The simple way to do this without going all object oriented and without resorting to a spaghetti bowl of global variables is to have the form that controls another form hold a reference to the form it needs to control. And it doesn’t matter whether it is the same form or another form class. Though in the latter case you will need to add the form’s unit to the interface section’s uses clause.
Then somewhere in the code, you will need to instantiate the form that you want to control.
For you can use
Application,Selfor nil. It depends on who you want the owner to be and that depends on who you want to control the lifetime ofFFormToControl. If it will simply exist until the application is closedApplicationwill do fine. If it should be freed when Form1 is freed, useSelfornil. Self will ensure that the VCL’s ownership system will take care of freeing. Using nil means that you will have to Free it yourself.Afterwards you can simply call methods and set properties of the TForm2 class:
If the second form can exist and be closed during the lifetime of a TForm1 instance, then you need to check whether the form has been instantiated before using any of its methods and properties:
You should also do that when FFormToControl is instantiated somewhere other than TForm1’s constructor (or OnCreate) and freed before TForm1’s destructor (or OnDestroy) as you can then never be sure whether FFormToControl is instantiated. In this case you are probably better of to use nil as the owner for FFormToControl and you should make sure that TForm1 is notified when FFormToControl is freed. For example by responding to the OnDestroy event and in its handler setting FFormToControl to nil.
Note:
When you do create and use a form from its own methods, you will have to be careful to avoid an endless loop of creating the next instance.