Let's say I have two forms Form1
and Form2
. Form1
contains two buttons, one that creates and displays Form2
and a button to close Form2
.
To create Form2
I use:
Form2 := TForm2.Create(Self);
Form2.Show;
How do I close Form2
from Form1
?
Call the Close
method of the other form:
Form2.Close;
Or if you want to destroy the object:
Form2.Free;
Or if you merely want to hide it:
Form2.Hide;
If the button that shows the form also creates it, then you need to make sure that you don't leak instances of the form. Imagine clicking that button multiple times, and making multiple instances of the form.
So perhaps you want code like this to create and show the form:
if not Assigned(Form2) then
Form2 := TForm2.Create(Self);
Form2.Show;
and code like this to close and destroy it:
Form2.Free;
Form2 := nil;