Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Delphi
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.
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 27-Jun-12 21:19pm
Edited 28-Jun-12 1:24am
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

In case the user closes the modal form, it is already gone.
 
Good luck!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Winston_D at 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 at 28-Jun-12 11:34am
   
Did you see this suggestion?
Winston_D at 28-Jun-12 13:12pm
   
thanks for the help
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
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!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
CreateModal(TResultForm);
Function will create your form even it MDIChild and return modal result.
  Permalink  
v2

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



Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 25 Oct 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100