Solved

catching events from mdi Child window

Posted on 2002-05-27
11
343 Views
Last Modified: 2010-08-05
Hi All,

I have a mdi app with several mdichild forms. On the mdi parent window i have a toolbar with a button that creates the child window and disables the button :

procedure TFrmMain.ToolButton1Click(Sender: TObject);
begin
 FrmCompanySearch := TFrmCompanySearch.Create(Self);
 FrmCompanySearch.Show;
 ToolBar1.Buttons[0].Enabled := false;
end;

The childwindow has his own onclose handler :

procedure TFrmCompanySearch.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := caFree;
  FrmCompanySearch := nil;
end;

Now the Question : How do i enable the toolbarbutton after the onclose event. So how can i catch the childwindow on close in my mdi parent form !

I hope that its a bit clear,
Thanks in advance,

Yuri
0
Comment
Question by:muldeyur
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7038551
you could send a wm_user+xxx message to your mainform
and handle the message there,
or
you could access directly the mainform

in your child-closemethod

meikl ;-)
0
 

Author Comment

by:muldeyur
ID: 7038607
how do i access the Mainform directly?
do you have a code example?

regards

Yuri
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 100 total points
ID: 7038626
simplified (from head)

mainform:

type tform1 ....

     public
       Procedure Handle_ChildClose(AChild : TForm);
    ....

implementation

procedure tform1.Handle_ChildClose(AChild : TForm);
begin
  if (aform is TFrmCompanySearch) then
    ToolBar1.Buttons[0].Enabled := true;
  //..maybe more childclasses to handle
end;

------------

childform

...

implementation
uses mainformunit; //!include this with your real unit name

....
procedure TFrmCompanySearch.FormClose(Sender: TObject;
 var Action: TCloseAction);
begin
 Action := caFree;
 FrmCompanySearch := nil;
 mainformname.handle_ChildClose(self);
end;

------

hope this helps

meikl ;-)


0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 7038635
oops there is a typo

this line
if (aform is TFrmCompanySearch) then

should be
if (aChild is TFrmCompanySearch) then
   
;-)
   
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 7038737
listening
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7038748
nick, the listener ? :-)
0
 
LVL 2

Expert Comment

by:freshman3k
ID: 7040505
hello!

This should help you:

private
 { Private declarations }
 FClientInstance : TFarProc;
 FPrevClientProc : TFarProc;
 procedure ClientWndProc(var aMessage: TMessage);

implementation

procedure TfrmMForm.FormShow(Sender: TObject);
begin
FPrevClientProc := Pointer(GetWindowLong(ClientHandle, GWL_WNDPROC));
FClientInstance := MakeObjectInstance(ClientWndProc);
SetWindowLong(ClientHandle, GWL_WNDPROC, LongInt(FClientInstance));
end;

procedure TfrmMForm.FormClose(Sender: TObject);
begin
SetWindowLong(ClientHandle, GWL_WNDPROC, LongInt(FPrevClientProc));
end;

procedure TfrmMForm.ClientWndProc(var aMessage: TMessage);

procedure DoDefault;
 begin
    with aMessage do
      Result := CallWindowProc(FPrevClientProc,ClientHandle, Msg, wParam,lParam);
 end;

begin
  if aMessage.Msg = WM_MDIDESTROY then begin
     
{aMessage.Child is handle of the Mdi Child being closed}

    if aMessage.Child = FrmCompanySearch.Handle then begin    
        ToolBar1.Buttons[0].Enabled := true;
        end;
   
    if aMessage.Child = OtherMdiChildForm.Handle then
begin  
        ToolBar1.Buttons[1].Enabled := true;          
        end;
   
    DoDefault; {do defualt processing}
   end;
  else
    DoDefault;  {do defualt processing}
  end;
end;


Good Luck! ;-)
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 7041013
Don't have time to answer all the time . So I "mark" :P the question and return latter . If is necesary .

So ...

const
  WM_DelChild = WM_User + 300;

type
  TMainForm = class(TForm)
...................................
private
    procedure CreateChild(const Name: string);
    procedure OnDelChild(var msg: TMessage); message WM_DelChild;  
    procedure OnChildDestroy(Sender: TObject);
.................

procedure TMainForm.OnDelChild(var msg: TMessage);
begin
  ShowMessage(' Just destroy me :( ');
end;

procedure TMainForm.OnChildDestroy(Sender: TObject);
begin
  PostMessage(TForm(MainForm).handle, WM_DelChild, 0, Integer(PChar('ChildName')));
end;

procedure TMainForm.CreateChild(const Name: String);
var
  Child: TChildForm;
begin
  Child := TChildForm.Create(Application);
  Child.Caption := Name;
  Child.OnDestroy := OnChildDestroy;
end;

0
 
LVL 9

Expert Comment

by:ginsonic
ID: 7041163
Sorry :

1. Child unit

const
  WM_CloseWin = WM_User + 300;

type
  TChildForm = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  end;

var
  ChildForm: TChildForm;

implementation

{$R *.DFM}

procedure TChildForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
var Buffer: array [0..99] of Char;
    s:string;
begin
  GetWindowText(TForm(Sender).Handle,buffer,100);
  s:=Buffer;
  PostMessage(Application.MainForm.handle, WM_CloseWin, 0, Integer(PChar(s)));
  Action := caFree;
end;

end.

2. Main unit

const
  WM_CloseWin = WM_User + 300;

type
  TMain = class(TForm)
...............
  private
    procedure OnCloseChild(var msg: TMessage); message WM_CloseWin;
    procedure CreateChild(const Name: string);
...............


procedure TMain.OnCloseChild(var msg: TMessage);
begin
  ShowMessage(Strpas(Pointer(msg.lparam)));
end;


0
 

Author Comment

by:muldeyur
ID: 7041170
Wow folks,

I'am going to try this when i get home,
Thanks to all that give advice,
will give the points as soon as i tried it!!

Regards,

Yuri
0
 

Author Comment

by:muldeyur
ID: 7042492
Thanks to all for the advice, but Kretzschmar was the quickest and also exactly what i Needed !

Thanks a lot,

Yuri
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question