Solved

route OnKeyPress event to procedure

Posted on 2002-05-28
2
430 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:Explorer060599
  • 2
2 Comments
 
LVL 14

Expert Comment

by:DragonSlayer
Comment Utility
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
0
 
LVL 14

Accepted Solution

by:
DragonSlayer earned 50 total points
Comment Utility
or unless you cheat... :)

code this in the beginning of your programme

type
  THackedWinControl = TWinControl;

and later in your code:

for i := 0 to t.Count - 1 do
  THackedWinControl(t[i]).OnKeyPress := ev_OnKeyPress;
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now