Any ideas why?
I thought it would cascade down since the sub-form is a component of the
mainform, for example, main form would execute event, then sub-form, then
component (if KeyPreview is True).
Here's an example:
Just add two forms and drop a TPanel on Form1, and a TEdit on Form2 and
assign these events:
The 'Form2 KeyDown()' message will never be shown.
{Form1}
procedure TForm1.FormCreate(Sender: TObject);
begin
KeyPreview := True;
Panel1.Align := alClient;
end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
ShowMessage('Form1 KeyDown()');
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Form2.Close;
Form2 := nil;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
Form2 := TForm2.Create(Self);
Form2.BorderStyle := bsNone;
Form2.Parent := Panel1;
Form2.WindowState := wsMaximized;
Form2.Show;
Form2.SetFocus;
end;
{Form2-----------}
procedure TForm2.FormCreate(Sender: TObject);
begin
KeyPreview := True;
end;
procedure TForm2.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
ShowMessage('Form2 KeyDown()');
end;
procedure TForm2.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
ShowMessage('Edit1 KeyDown()');
end;
Jure
Matt Peebles <m...@collinscomputing.com> wrote in message
news:7mitn3$3i...@forums.borland.com...
Plus, now I'm noticing that if the ActiveControl has a OnKeyDown event,
Form1's OnKeyDown event is not firing (only Edit1's event is), and I know
that should happen before the ActiveControl's event fires (at least that's
what the KeyPreview help says should happen).
Jure <dz...@hotmail.com> wrote in message
news:7mk0tb$4s...@forums.borland.com...
I cannot reproduce this with D4.03, sorry. The form1.formkeydown fires as
expected. Are you using an unpatched Delphi 4?
Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
The problem is that I expected the Form2(sub-form) OnKeyDown to fire after
the Form1 (parent) OnKeyDown. Or is there a reason why the sub-form
wouldn't fire the event?
Peter Below (TeamB) <10011...@compuXXserve.com> wrote in message
news:VA.000033bf.00faf511@noname...
The help says: "If KeyPreview is True, keyboard events occur on the form
before they occur on the active control."
So, I expect the forms' events to fire first and they are not. Not to
mention that Form2's KeyDown event never fires. I did not know if there was
a reason or if this is a bug?
Thanks for looking into this though!
Matt Peebles <m...@collinscomputing.com> wrote in message
news:7mlnau$66...@forums.borland.com...
> I am using D4.03.
>
> The problem is that I expected the Form2(sub-form) OnKeyDown to fire after
> the Form1 (parent) OnKeyDown. Or is there a reason why the sub-form
> wouldn't fire the event?
>
> Peter Below (TeamB) <10011...@compuXXserve.com> wrote in message
> news:VA.000033bf.00faf511@noname...
> >
No, it is not a bug. If you parent a form to some control it ceases to be a
top-level window and becomes a control. The code in the VCL (in
TWinControl.CNKeyDown/CNKeyUp/CNChar) uses the GetParentForm function to find
the parent form of the control that received the key message, checks if that
forms KeyPreview is true and fires the forms key event if it is. GetParentForm
walks up the parent chain until it finds a TWincontrol that has no Parent and
is a TCustomForm. This search skips Form2 in your case since it has a parent.
A form parented to some control does not work like a parentless form in
several ways. It will not get Windows messages like WM_ACTIVATE and WM_CLOSE,
for example, and thus not fire its OnActivate, OnDeActivate, OnCloseQuery and
OnClose events, which depends on these messages.