Board index » delphi » Help with "components on the fly"

Help with "components on the fly"

I've created a componenet on the fly (ie I didn't "put" it on a form),
and I want to access an event handler (ie OnSomething()), any ideas on
how to do this?
-TIA

 

Re:Help with "components on the fly"


Quote
In article <5huhba$...@uwm.edu>, Dave Laumann <d...@tiptech.com*> wrote:

]-I've created a componenet on the fly (ie I didn't "put" it on a form),
]-and I want to access an event handler (ie OnSomething()), any ideas on
]-how to do this?
]--TIA

here's how you'd do it with a TButton....

Procedure TMyForm.MyOnClickHandler(Sender: TObject);
  BEGIN
  {= a method of your form containing the code you wish to =}
  {= have executed by your dynamically created TButton...  =}
  END;

MyButton := TButton.Create(MyForm);
    {= AnOwner will frequently be a form, but it can just as easily =}
    {= be another component (e.g., a panel or a notebook) too       =}
with MyButton do
  BEGIN
    Parent := MyForm;
        {= gotta set the parent property or the dynamically created    =}
        {= component won't appear on the screen...the position of      =}
        {= MyButton is relative to the coordinate system of its parent =}
    top := 24;
    left := 12;
    caption := 'Hey You!';
    OnClick := MyForm.MyOnClickHandler;
  { and so on for all other properties and events that need initializing }
  END;

the event handlers are really just properties, and you set 'em
just like any other properties...the thing that's important is
the *type* of the routine you assign...in particular, be sure
your routines are *method pointers*

for example, the OnClick event is a TNotifyEvent, declared as
follows...

    TNotifyEvent = procedure (Sender: TObject) of object;

the critical part is the "of object" designation, which means that
any procedure you wish to have recognized as a TNotifyEvent must be
a method of some object.  perhaps the easiest way to accomplish this
is to make your routines methods of the form on which you place your
dynamically created components...

for more detail, search the on-line help for the topics "procedural
types", "global procedure pointers", and "method pointers".

Mark Vaughan

Re:Help with "components on the fly"


Quote
In article <5huhba$...@uwm.edu>, d...@tiptech.com* wrote:
>I've created a componenet on the fly (ie I didn't "put" it on a form),
>and I want to access an event handler (ie OnSomething()), any ideas on
>how to do this?
>-TIA

<object>.<event> := <handler procedure>;

For example:

MyButton:= TButton.Create( Form1);
MyButton.OnClick := ButtonClickProc;
^^^^^^^^^^^^^^^^
elsewhere...

procedure Form1.ButtonClickProc(Sender: TObject);
begin
...

end;

hth

-- luu

Re:Help with "components on the fly"


Quote
Dave Laumann wrote:
> I've created a componenet on the fly (ie I didn't "put" it on a form),
> and I want to access an event handler (ie OnSomething()), any ideas on
> how to do this?

But1.OnClick := Form1.Btn_handler;

Where Btn_handler has the standard parameters for an OnClick handler.

Andrew

Other Threads