Solved

catching events from mdi Child window

Posted on 2002-05-27
11
335 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb submit form as a POST request 4 259
Strange behavior when a form is closed 6 50
how can i search if string exist in array ? 3 49
CheckListBox usage 3 49
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now