Solved

route OnKeyPress event to procedure

Posted on 2002-05-28
2
433 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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