Delphi, Pointers to procedures

This code works :


     bde_name.OnKeyDown:=bde_nameKeyDown;

But when i Try to do the same thing with a pointer it fails:

procedure Tfrm_Jobbsiter.Button3Click(Sender: TObject);
    var
      p : TProcedure;
begin

     p:=bde_nameKeyDown;
     bde_name.OnKeyDown:=p;
end;

How is this done correctly?
Paer ToernellAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SuperdaveCommented:
p should be declared "procedure of object" instead of TProcedure
Paer ToernellAuthor Commented:
Theax, but then i get "E2009 Incompatible types: 'Parameter lists differ"
-
In the working example i dont have to handle the parameters, and that is the point :P
jimyXCommented:
You are not going to handle them at this point but you have to declare the procedural type with parameters that match the procedure(s) that is going to be assigned, the following example if for assigning the Edit1KeyDown procedure to a variable procedure:

var
  p : Procedure (Sender: TObject; var Key: Word;Shift: TShiftState) of object;
begin
  p := Edit1KeyDown;
  Edit1.OnKeyDown := p;

Open in new window



The parameters should be matching to the procedure that is used otherwise you have error.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

cyberkiwiCommented:
Some pointers (no pun intended)

An OnKeyDown event has the signature
Procedure(Sender: TObject; var Key: Word; Shift: TShiftState) Of Object

The "of object" part is significant, because it makes it a member procedure instead of a standalone, static (non-class) procedure such as SysUtils.Copy.

You attempted to declare p using     var p : TProcedure;

The implied signature is
TProcedure();

meaning it is neither "of object", nor has the right signature.
Either declare a type of a matching signature (or use the built in type TKeyDown)

procedure Tfrm_Jobbsiter.Button3Click(Sender: TObject);
    var p : TKeyDown;
begin
     p:=bde_nameKeyDown;
     bde_name.OnKeyDown:=p;
end;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Paer ToernellAuthor Commented:
Very nice, but I guess the TKeyDown is not implemented in my old rad studio 2007.
jimyXCommented:
@ cyberkiwi,

Where did you come with that TKeyDown from? I do not think that's in Delphi at all.

If it's there what is the ideal use of it?
Ephraim WangoyaCommented:
@cyberkiwi
I think its a typo, should be TKeyEvent
FOnKeyDown: TKeyEvent

All these are defined in unit controls

TKeyEvent = procedure(Sender: TObject; var Key: Word; Shift: TShiftState) of object;

TKeyPressEvent = procedure(Sender: TObject; var Key: Char) of object;
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.