Search code examples
delphidelphi-7

how to create MDI form in delphi


How can I solve this problem "Cannot Create Forms. No MDI forms are currently active". I want to make a simple program that wod require a Login Form before it can acess the main form. I got three forms: Main Form (MDI Form), Form2 (MDIChild) and Login Form (Normal). Login form would appear first then the Main Form, then when I try to call on Form 2 from the Main form, an error would display "Cannot Create Forms. No MDI forms are currently active". I am using Delphi 7 on windows XP. I'm a beginner. Thank you very much sir.


Solution

  • It sounds like you're letting your LoginForm be auto-created, and it's being created first. This won't work, because the first form created by Application.CreateForm in the project file becomes the Application.MainForm. In order by be an MDI application, the MainForm must be a MDI parent window.

    The solution is usually to not auto-create your login form, and instead create it yourself. To do so, you need to edit your .dpr file (Project->View Source from the IDE's main menu).

    Your project source should look something like this now (obviously, using your classes in the Application.CreateForm calls):

    begin
      Application.Initialize;
      Application.CreateForm(TLoginForm, LoginForm);
      Application.CreateForm(TMainForm, MainForm);
      Application.CreateForm(TChildForm, ChildForm);
      Application.Run;
    end.
    

    You need to modify it so that the LoginForm isn't created first.

    var
      LoginOK: Boolean = False;
    
    begin
      LoginForm := TLoginForm.Create(nil);
      try
        // Show login form. When it closes, see if login worked.
        LoginForm.ShowModal;
        LoginOK := LoginForm.CanLogin; // However you know login is OK or not here
      finally
        LoginForm.Free;
      end;
      if not LoginOK then
        Halt;    // Login failed - terminate application.
      Application.Initialize;
      Application.CreateForm(TMainForm, MainForm);
      {
         I normally do not auto-create anything but the main form
         and maybe a datamodule (which you **can** autocreate first -
         it is not a form), but a MDI application is pretty useless
         without at least one open child window, IMO.
      }
      Application.CreateForm(TChildForm, ChildForm);
      Application.Run;
    end.