Link to home
Start Free TrialLog in
Avatar of keithcsl
keithcsl

asked on

Trapping for Ctrl-F4

Hi

I have a MDI applicatin with several child forms. How do I prevent the user from pressing Ctrl-F4 to close the child for (similarly Ctrl-F6 to switch between forms)?

Regards
Keith
ASKER CERTIFIED SOLUTION
Avatar of viktornet
viktornet
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of keithcsl
keithcsl

ASKER

victornet

Hi, have you tried your code and got it going? I wrote the exact same code but have failed to trap it. I might be doing it wrong:

In the OnCloseQuery event:
  if flgCannotClose = True then
    CanClose := False;

In the KeyDown event of the form
  if (Shift = [ssctrl]) AND (key = VK_F4) then
    flgCannotClose = True;

Regards
Keith
Victornet

I have noticed that the OnCloseQuery is called before the OnKeyDown, that's why I can't trap the Ctrl-F4

It works for Ctrl-F3, etc

Regards
Keith
Hello Keith =)

Here is what you do......
-----------
var
  flgCannotClose : boolean = False;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  flgCannotClose := True = (Shift = [ssctrl]) AND (key = VK_F4);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
   if flgCannotClose = True then
     Action := caNone;
end;

procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  flgCannotClose := False;
end;
----------
Regards,
Viktor Ivanov
Oh boy, I think it must have been the late nights that have made me a little blind.

Viktornet, thanks to your code above, I have just realised that to avoid the forms from closing, all I have to do is to set
  Action := caNone
in the OnClose event of the form

Thanks a lot

Regards
Keith
No prob :-)

Regards,
Viktor Ivanov