Solved

route OnKeyPress event to procedure

Posted on 2002-05-28
2
432 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
ID: 7041130
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
ID: 7041133
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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