Solved

route OnKeyPress event to procedure

Posted on 2002-05-28
2
436 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Title # Comments Views Activity
How to open a new windows chrome resized and disabled the toolbar? 3 237
Delphi 2 92
TEMBEDDEDWB how can i change its user agent ? 8 106
IP without any Dots 1 100
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

710 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