Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I am trying to make a multiforms application by having only the main form created on start up and creating each new additional form dynamically as needed by creating that form object, using it as a modal and once that form's purpose is complete, it is freed from memory after it is closed. Only my main form is 'auto-created', all additional forms are set as 'available' in the the project options.

Here is sample code.

results form is declared as a global variable of TResultForm.
Delphi
procedure TMainForm.Button1Click(Sender: TObject);
begin
  ResultsForm := TResultForm.Create(self);
  try
    ResultsForm.ShowModal;
  finally
    ResultsForm.Free;
  end;
end;


The moment I call this method by clicking on the button I recieve an error message:

'Cannot make a visible window modal.'

On that form (ResultsForm) I have databases being linked to that form and displayed. Code for that works, no problems. Since trying this new sample code sourced from Embarcadero's help tool, only the controls on the new form (ResultsForm) are loaded, no explicitly added code on that form initiates.
Posted
Updated 23-Jul-22 23:42pm
v4

Someone showed me that there was just one small key that I didn't do. The code:

Note that ResultForm may be declared as an global variable of TResultForm or as a local variable. Not yet sure if a difference arrises later though.
Delphi
procedure TMainForm.Button1Click(Sender: TObject);
begin
  ResultsForm := TResultForm.Create(self);
  try
    ResultsForm.ShowModal;
  finally
    ResultsForm.Free;
  end;
end;

for dynamically creating a new form is sound but one should take care to note that the dynamically created form MUST HAVE ITS VISIBILITY SET TO FALSE. Else an error like:

'Cannot make a visible window modal.'

will appear.

Probably seems very logical, hopefully nobody else makes this same stupid mistake!
 
Share this answer
 
Comments
Member 14705295 17-Jul-20 6:57am    
This was exactly my problem, thank you!
In case the user closes the modal form, it is already gone.

Good luck!
 
Share this answer
 
I think the message is reasonably clear. A modal dialog is used as a temporary Window which pops up and either displays some information or asks for input from the user. It should not be a global object, but created only when needed and destroyed after use. From your description it seems like you are trying to use an existing Window and somehow make it modal.
 
Share this answer
 
Comments
Winston_D 28-Jun-12 11:20am    
To clarify my situation. ResultForm is created only when it is needed for results and destroyed immedietly after it is closed. So it is temporary. Even if I declare ResultForm as a local object (locally declared variable), I still get the same error message.

If I use 'form.show' method instead of '.showmodal', I still have the same error message appearing. I use modal because that form needs to be a modal.
Richard MacCutchan 28-Jun-12 11:34am    
Did you see this suggestion?
Winston_D 28-Jun-12 13:12pm    
thanks for the help
Delphi
function TMainForm.CreateModal(ModalForm: TFormClass): Integer;
begin
  with ModalForm.Create(Self) do
  try
    FormStyle := fsNormal;
    WindowState:=wsNormal;
    Hide;
    if (Self.Owner is TWinControlClass)and(Self.FormStyle=fsMDIChild) then
      begin//if Self is MDIChild then set positions by main window
        Top:=TWinControl(Self.Owner).Top+TWinControl(Self.Owner).Height-TWinControl(Self.Owner).ClientHeight+Self.Top;
        Left:=TWinControl(Self.Owner).Left+TWinControl(Self.Owner).Width-TWinControl(Self.Owner).ClientWidth+Self.Left;
      end
    else//if Self is MainWindow or sequence of ShowModal-Forms
      begin
        Top:=Self.Top;
        Left:=Self.Left;
      end;
    Width:=Self.Width;
    Height:=Self.Height;
    Result:=ShowModal;
  finally
    Free;
  end;
end;

Call
Delphi
CreateModal(TResultForm);

Function will create your form even it MDIChild and return modal result.
 
Share this answer
 
v2
I was getting the same error. Took ages to resolve. At some point, I had accidentally set the form property I was going to call modally to Enabled=False, setting back to Enabled=True fixed the issue
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900