I want to when Tform2 is created then show a message to user.
I use this code, but not work well.
procedure TForm1.Button1Click(Sender: TObject);
var
a:TForm2;
begin
if a=nil then
begin
a := TForm2.Create(Self);
a.Show;
end
else
begin
showmessage('TForm2 is created');
end;
end;
That’s because you declare
aas a local variable. Each time you enterTForm1.Button1Clickthis variable will be brand new and uninitialized even though there might still be a Form2. That means that the check for nil won’t even work.You should either:
aa global (like the Form2 global you get when you first create a form)apart of the declaration of Form1 (you main form?) or a datamodule of other class that lives throughout your entire program.Screen.Formsto see if you got a Form2 in there.[edit]
Like this: