1
votes

i have create mainform (auto create form) and Form1 (available form). the method that i use to call form1 is like this

procedure Tmainform.Button2Click(Sender: TObject);
var
f : Tform1;
begin
f:=Tform1.create(self);
f.parent:=Tabsheet1;
f.visible:=true;
f.align:=alClient;
end;

the question is why KeyPreview in Form1 does not work, even though I have activated his KeyPreview be true?

1
Presumably because it's not designed for forms contained inside other forms.David Heffernan
Why don't you use TFrames?alzaimar
I cannot duplicate your problem.Sertac Akyuz

1 Answers

2
votes

in function TWinControl.DoKeyDown(var Message: TWMKey): Boolean; the call is delegated to the parent if existing.

The Procedure

procedure TWinControl.KeyDown(var Key: Word; Shift: TShiftState);
begin
  if Assigned(FOnKeyDown) then FOnKeyDown(Self, Key, Shift);
end;

will not be called if Form is parented

function TWinControl.DoKeyDown(var Message: TWMKey): Boolean;
var
  ShiftState: TShiftState;
  Form, FormParent: TCustomForm;
  LCharCode: Word;
begin
  Result := True;
  { First give the immediate parent form a try at the Message }
  Form := GetParentForm(Self, False);
  if (Form <> nil) and (Form <> Self) then 
  begin
    //  >> -- the DoKeyDown of the parent (not of your form) will be called 
    if Form.KeyPreview and TWinControl(Form).DoKeyDown(Message) then
      Exit;
    { If that didn't work, see if that Form has a parent (ie: it is docked) }
    if Form.Parent <> nil then 
    begin
      FormParent := GetParentForm(Form);
      if (FormParent <> nil) and (FormParent <> Form) and FormParent.KeyPreview and
          TWinControl(FormParent).DoKeyDown(Message) then
        Exit;
    end;
  end;
......