Link to home
Start Free TrialLog in
Avatar of Explorer060599
Explorer060599

asked on

route OnKeyPress event to procedure

hi experts, i would like to route all TWinControl control's OnKeyPress event to my local procedure as follow. why it's error "undeclared identifier: 'OnKeyPress' ? isn't TWinControl the base class of this event ?

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
  private
    procedure ev_OnKeyPress(Sender: TObject; var Key: Char);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
  wc : TWinControl;
  t : TList;
  i : Integer;
begin
  t := TList.Create;
  try
    t.Add(Edit1);
    t.Add(Button1);
    for i := 0 to t.Count -1 do
      TWinControl(t[i]).OnKeyPress := ev_OnKeyPress;
  finally
    t.Free;
    t := nil;
  end;
end;
procedure TForm1.ev_OnKeyPress(Sender: TObject; var Key: Char);
begin

end;

end.
Avatar of DragonSlayer
DragonSlayer
Flag of Malaysia image

Nope, you can't... OnKeyPress is a property that is in the Protected section of the hierarchy, until it reached the last level, e.g. TEdit, TButton, etc.

So you might have to do this

if (t[i] is TEdit) then
  TEdit(t[i]).OnKeyPress := ev_OnKeyPress
else ...



HTH
DragonSlayer
ASKER CERTIFIED SOLUTION
Avatar of DragonSlayer
DragonSlayer
Flag of Malaysia 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