I have VCL application in delphi. One main form and many child forms.
How to ensure only one form opened at a time. In other words, If a form is opened, it will close previous form. Or, if user try to open form that same with previous, it will do nothing.
Code to open form in my main form:
procedure TFMainForm.OpenForm(const classname: string);
var
c: TPersistentClass;
f: TForm;
begin
c := GetClass(classname);
if c <> nil then
begin
f := TForm(TControlClass(c).Create(nil));
f.Parent := Self;
f.Show;
end;
end;
The child form is self-freed on close event.
If you make ‘f’ a variable in your mainform, instead of a local variable, you will have a reference to the currently open form. You can use that reference to close that form or to check its class.
Two notes:
As an alternative, you could make the child forms modal (use ShowModal instead of Show), but that would block access to the main form when the child form is open.