Cannot create form. No MDI forms are currently active.

Hi,
It's been a while since I last worked on a Delphi project and I'm
quite rusty in Delphi.
I have a MDI form and a child form.
In the main MDIForm, I have the following that runs when
a configuration file isn't found:
       cd := TConfigDialog.Create(nil);
       cd.ShowModal;
       PostMessage(self.handle, WM_CLOSE, 0, 0);
       Screen.Cursor := crDefault;
But once it hits the first line, it gives me the warning as
mentioned in the subject.  I wrote this code a while back
and I'm not exactly sure what the PostMessage() part does
right now; but I think it closes the main form as well
when the configuration dialog closes.
Can someone point out which part of the project configuration
do I set the MDI form active?
Thanks
Edmund
0
Edmund
1/5/2015 9:31:41 AM
📁 embarcadero.delphi.general
📃 4258 articles.
⭐ 0 followers.

💬 4 Replies
👁️‍🗨️ 2934 Views


Edmund wrote:
> In the main MDIForm, I have the following that runs when a
> configuration file isn't found:
<snip>
> But once it hits the first line, it gives me the warning as
> mentioned in the subject. 
The only way you can get that particular error on that particular line of 
code is if either:
1) TConfigDialog's FormStyle property is set to fsMDIChild and TConfigDialog 
is being created before the MainForm (or its MDI client window) has been 
created, such as in the MainForm's OnCreate event.
2) the MainForm's FormStyle is not set to fsMDIForm. 
If TConfigDialog's FormStyle really is set to fsMDIChild, keep in mind that 
you can't use ShowModal() with an fsMDIChild form, it will ignore the request 
and raise an EInvalidOperation exception.
> I wrote this code a while back and I'm not exactly sure what
> the PostMessage() part does right now;
It is causing the calling Form (which I assume is the MainForm) to close 
itself.  If I had to guess, I would think that if the config file is missing, 
you are displaying the dialog, and then terminating the app afterwards.
> but I think it closes the main form as well when the configuration dialog 
closes.
If Self is the MainForm, then yes.
> Can someone point out which part of the project configuration do I set
> the MDI form active?
The MainForm's FormStyle has to be set to fsMDIForm, and the MainForm has 
to be fully created and assigned to the Application.MainForm property before 
any fsMDIForm forms can be created.
--
Remy Lebeau (TeamB)
0
Remy
1/5/2015 6:46:53 PM
Edmund Wong wrote:
> Hi,
> 
> It's been a while since I last worked on a Delphi project and I'm
> quite rusty in Delphi.
> 
> I have a MDI form and a child form.
> 
> In the main MDIForm, I have the following that runs when
> a configuration file isn't found:
> 
>        cd := TConfigDialog.Create(nil);
>        cd.ShowModal;
>        PostMessage(self.handle, WM_CLOSE, 0, 0);
>        Screen.Cursor := crDefault;
> 
> But once it hits the first line, it gives me the warning as
> mentioned in the subject.  I wrote this code a while back
> and I'm not exactly sure what the PostMessage() part does
> right now; but I think it closes the main form as well
> when the configuration dialog closes.
Check the formstyle of the TConfigDialog form class. If the formstyle
is fsMdiChild set it to fsNormal. The code is probably executing before
Application.Mainform has been set to the main form reference, and in
this situation you cannot create MDi child forms yet since they cannot
find their parent.

-- 
Peter Below (TeamB)
0
Peter
1/5/2015 6:48:02 PM
Remy Lebeau (TeamB) wrote:
> Edmund wrote:
>
>> In the main MDIForm, I have the following that runs when a
>> configuration file isn't found:
> <snip>
>> But once it hits the first line, it gives me the warning as
>> mentioned in the subject.
>
> The only way you can get that particular error on that particular line of
> code is if either:
>
> 1) TConfigDialog's FormStyle property is set to fsMDIChild and TConfigDialog
> is being created before the MainForm (or its MDI client window) has been
> created, such as in the MainForm's OnCreate event.
Ah.  TConfigDialog's FormStyle is set to fsStayOnTop.  And yeah, that
code is in the MainForm's onCreate event.
>> Can someone point out which part of the project configuration do I set
>> the MDI form active?
>
> The MainForm's FormStyle has to be set to fsMDIForm, and the MainForm has
> to be fully created and assigned to the Application.MainForm property before
> any fsMDIForm forms can be created.
Hmm.  I was hoping to run a config dialog if the configuration file
doesn't exist.  But apparently, this isn't the right way of doing it.
Particularly in the onCreate event.
I think I'll need to use another way of doing this.
Thanks Remy!
0
Edmund
1/6/2015 12:17:12 AM
Edmund wrote:
> TConfigDialog's FormStyle is set to fsStayOnTop.
Then you cannot be getting the "No MDI forms are currently active" error 
on the line of code you indicated.  Only fsMDIChild Forms can raise that 
particular error:
{code}
procedure TCustomForm.CreateWindowHandle(const Params: TCreateParams);
var
  CreateStruct: TMDICreateStruct;
  NewParams: TCreateParams;
begin
  if (FormStyle = fsMDIChild) and not (csDesigning in ComponentState) then 
// <-- note the FormStyle check!
  begin
    if (Application.MainForm = nil) or
      (Application.MainForm.ClientHandle = 0) then
      raise EInvalidOperation.Create(SNoMDIForm); // <-- exception raised 
here!
  ...
end;
{code}
No other code in the VCL raises that particular error message.  This is the 
only spot.
> Hmm.  I was hoping to run a config dialog if the configuration
> file doesn't exist.
You can certainly do that.  I would do it differently, though.  Instead of 
using the MainForm's OnCreate event, I would do the check in the .dpr file 
itself before creating the MainForm at all, eg:
{code}
Application.Initialize;
if not FileExists(ConfigFileName) then
begin
  cd := TConfigDialog.Create(nil);
  try
    if cd.ShowModal <> mrOk then
      Exit;
  finally
    cd.Free;
  end;
end;
Application.CreateForm(TForm1, Form1);
Application.Run;
{code}
--
Remy Lebeau (TeamB)
0
Remy
1/6/2015 12:30:56 AM
Reply: