Piotr
Piotr

Reputation: 33

Working with 2 active modalforms in Delphi

I need a help ...

I have two separated modal forms in Delphi VCL Forms application. Based on the solution from: How can I make a form that is not disabled when another form is shown modally?

I use EnableWindow(Self.Handle, True) and it works perfect: I can open and edit the first form and without closing it, open the second form. Both are active and editable.

The problem starts when I try to close first modal form without closing the second. It waits until the second one is closed.

Does exist any way to forced closing the first modal form (except the Self.Free because I need some information from model form in parent form) having the second one still open?

The code is quite simple:

Main form (with Button1):

procedure TfrMainForm.WMEnable(var _Msg: TWMEnable);
begin
  inherited;
     if not _Msg.Enabled and (Application.ModalLevel > 0) then Begin
          EnableWindow(Self.Handle, True);
     End;
end;


procedure TfrMainForm.Button1Click(Sender: TObject);
Var
     tmpSubForm: TfrSubForm;
begin
     tmpSubForm := TfrSubForm.Create(Self);
     Try
          tmpSubForm.ShowModal;
          //get some information from tmpSubForm
     Finally
          tmpSubForm.Free;
     End;
end;

"Child" form (do nothing - only open):

procedure TfrSubForm.WMEnable(var _Msg: TWMEnable);
begin
  inherited;
     if not _Msg.Enabled and (Application.ModalLevel > 0) then Begin
          EnableWindow(Self.Handle, True);
     End;
end;

Upvotes: 1

Views: 812

Answers (1)

USauter
USauter

Reputation: 335

You can work in every window, regardless of whether it is a parent window or a child window.

DFM-Datei:

object frm: Tfrm
  Left = 0
  Top = 0
  Caption = 'frm'
  ClientHeight = 194
  ClientWidth = 283
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnClose = FormClose
  PixelsPerInch = 96
  TextHeight = 13
  object btnShow: TButton
    Left = 104
    Top = 132
    Width = 75
    Height = 25
    Caption = 'Show'
    TabOrder = 0
    OnClick = btnShowClick
  end
  object txt: TEdit
    Left = 8
    Top = 40
    Width = 121
    Height = 21
    TabOrder = 1
  end
end

Unit:

unit Unit1;

interface

uses
  System.SysUtils, System.Variants, System.Classes,
  Vcl.Controls, Vcl.Forms, Vcl.StdCtrls;

type
  Tfrm = class(TForm)
    btnShow: TButton;
    txt: TEdit;
    procedure btnShowClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
  protected
     procedure CreateParams(var Params: TCreateParams); override;
  public
    { Public-Deklarationen }
  end;

var
  frm: Tfrm;

implementation

{$R *.dfm}

procedure Tfrm.CreateParams(var Params: TCreateParams);
var
  Lfrm: TForm;
begin
  inherited CreateParams(Params);

  Lfrm := nil;
  if Owner is TControl then
    Lfrm := (GetParentForm(Owner as TControl, false) as TForm);
  if not Assigned(Lfrm) then
    Lfrm := Application.MainForm;

  if Assigned(Lfrm) and (Lfrm <> Self) then
  //Bind the child window to its parent window.
      Params.WndParent := Lfrm.Handle;
end;


procedure Tfrm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if Self <> Application.MainForm then
  //Action caHide -> caFree
    Action := caFree;
end;

procedure Tfrm.btnShowClick(Sender: TObject);
var
  Lfrm: Tfrm;
begin
  Lfrm := Tfrm.Create(Self);
  Lfrm.Top  := Top  + 10;
  Lfrm.Left := Left + 10;
  //Since you want to work in the parent window, the child window must not 
  //   display this in a showmodal manner!
  Lfrm.Show;
end;

end.

Upvotes: 1

Related Questions