Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

OnKeydown not firing when a form is set inside another form

953 views
Skip to first unread message

Matt Peebles

unread,
Jul 14, 1999, 3:00:00 AM7/14/99
to
I am binding a form inside a TPanel on the main form, kind of a Outlook
style app, and I have noticed that the OnKeydown event for the sub-form will
not fire, only the main form's key event will fire. Although, a conponent
on the sub-form will acknowledge the key events.

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

unread,
Jul 15, 1999, 3:00:00 AM7/15/99
to
Can you tell with which version of Delphi you're working with ?
If you're working with delphi4, this code works, cause I've tried it out!

Jure

Matt Peebles <m...@collinscomputing.com> wrote in message
news:7mitn3$3i...@forums.borland.com...

Matt Peebles

unread,
Jul 15, 1999, 3:00:00 AM7/15/99
to
I'm sorry, I should have been more specific. I am using Delphi 4. I did
notice the Form2 message when the form is first shown and Form2 has focus.
But my problem is that once the focus is on the TEdit, the KeyPreview in
Form2 is being ignored and the OnKeyDown is not fired again.

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...

Peter Below (TeamB)

unread,
Jul 15, 1999, 3:00:00 AM7/15/99
to
In article <7mitn3$3i...@forums.borland.com>, Matt Peebles wrote:
> I am binding a form inside a TPanel on the main form, kind of a Outlook
> style app, and I have noticed that the OnKeydown event for the sub-form will
> not fire, only the main form's key event will fire. Although, a conponent
> on the sub-form will acknowledge the key events.
>
> Any ideas why?
>

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!


Matt Peebles

unread,
Jul 15, 1999, 3:00:00 AM7/15/99
to
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...

Matt Peebles

unread,
Jul 15, 1999, 3:00:00 AM7/15/99
to
Instead using ShowMessage I decided to use a TMemo and add strings to it for
each event, and I learned the TEdit fires first and then Form1 (Main form).
Form2 (sub-form that contains the TEdit) never fires the event.

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...
> >

Peter Below (TeamB)

unread,
Jul 16, 1999, 3:00:00 AM7/16/99
to
In article <7mlpm3$67...@forums.borland.com>, Matt Peebles wrote:
> 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?
>

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.

0 new messages