Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: Cannot create form. No MDI forms are currently active.



Permlink Replies: 4 - Last Post: Jan 5, 2015 4:30 PM Last Post By: Remy Lebeau (Te...
Edmund Wong

Posts: 154
Registered: 10/26/02
Cannot create form. No MDI forms are currently active.
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 5, 2015 1:31 AM
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
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Cannot create form. No MDI forms are currently active.
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 5, 2015 10:46 AM   in response to: Edmund Wong in response to: Edmund Wong
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)
Edmund Wong

Posts: 154
Registered: 10/26/02
Re: Cannot create form. No MDI forms are currently active.
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 5, 2015 4:17 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
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!
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Cannot create form. No MDI forms are currently active.
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 5, 2015 4:30 PM   in response to: Edmund Wong in response to: Edmund Wong
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:

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;


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:

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;


--
Remy Lebeau (TeamB)
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: Cannot create form. No MDI forms are currently active.
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 5, 2015 10:48 AM   in response to: Edmund Wong in response to: Edmund Wong
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)

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02