Board index » delphi » Assigning an event handler to dynamically created component?

Assigning an event handler to dynamically created component?

Here is a problem...

I've created a component dynamically, and I want to to access its events. How?
I've done something like this...

var x:TDBGrid;

procedure bar;
begin
        ...
        x.Create(self);
        x.Parent:=self;
        .... { set some other properties...}
        x.OnKeyDown:=foo;
end;

procedure foo;
begin
end;

I did some experimenting with the code but nothing I could think of helped...
Oh yeah, I'm using D1...
Something tells me this is trivial question, but I can't figure it out, and
it's kinda urgent, so if anyone knows...
TIA

--
ms...@rijeka.riteh.hr aka Sasha Matijasic

 "Great spirits have always encountered wild
  opposition from mediocre minds."
        -- Albert Einstein

 

Re:Assigning an event handler to dynamically created component?


In article <563169$...@argos.tel.hr>,
   ms...@rijeka.riteh.hr (Sasha Matijasic) wrote:
]-Here is a problem...
]-
]-I've created a component dynamically, and I want to to access its events. How?
]-I've done something like this...
]-
]-var x:TDBGrid;
]-
]-procedure bar;
]-begin
]-        ...
]-        x.Create(self);
]-        x.Parent:=self;
]-        .... { set some other properties...}
]-        x.OnKeyDown:=foo;
]-end;
]-
]-procedure foo;
]-begin
]-end;
]-
]-I did some experimenting with the code but nothing I could think of helped...
]-Oh yeah, I'm using D1...
]-Something tells me this is trivial question, but I can't figure it out, and
]-it's kinda urgent, so if anyone knows...

procedure foo needs to be the right kind of procedure.  if you're going
to assign it to the OnKeyDown event, foo should be a TKeyEvent...

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

the "of object" bit at the end of the declaration means that foo  will
also have to be a method of some object.  add something like this to your
program...

type
  TMyObject = CLASS
     procedure foo(Sender: TObject; var Key: Word; Shift: TShiftState);
   END;

procedure TMyObject.foo(Sender: TObject; var Key: Word; Shift: TShiftState);
  begin
    // do something with sender and/or key and/or shift
  end.

now the assignment
  X.OnKeyDown := MyObject.foo;
will do what you expect.  (in practice I find it's usually easiest to
makee procedures like foo methods of some form...)

Mark Vaughan

]-TIA
]-
]-

Re:Assigning an event handler to dynamically created component?


Procedures that respond to events need to take a sender param.  Procedure FOO
needs to be declared and implemented as follows:

procedure foo( Sender: TObject);
begin
...
end;

--
Joe Healy - Tampa / St Petersburg FL

In article <563169$...@argos.tel.hr>, ms...@rijeka.riteh.hr says...

Quote

>Here is a problem...

>I've created a component dynamically, and I want to to access its events. How?
>I've done something like this...

>var x:TDBGrid;

>procedure bar;
>begin
>        ...
>        x.Create(self);
>        x.Parent:=self;
>        .... { set some other properties...}
>        x.OnKeyDown:=foo;
>end;

>procedure foo;
>begin
>end;

>I did some experimenting with the code but nothing I could think of helped...
>Oh yeah, I'm using D1...
>Something tells me this is trivial question, but I can't figure it out, and
>it's kinda urgent, so if anyone knows...
>TIA

>--
>ms...@rijeka.riteh.hr aka Sasha Matijasic

> "Great spirits have always encountered wild
>  opposition from mediocre minds."
>        -- Albert Einstein

Other Threads