Board index » delphi » OnClick Event with a run-time create button

OnClick Event with a run-time create button

Hello,

who can help me to assign an procedure to an Button create in Run Time with

btn := TButton.Create(Self);
btn.onclick := procedure xxx;

the compiler return an error with incompatibility on TNotify Event and
Procedure ?

can you help me ?

thanks

 

Re:OnClick Event with a run-time create button


Hi,

    I've found that the easiest way to attach events to components created at
 runtime, is to place the component on the form, click the event handlers you
want, insert some code in each event handler (just begin and end will do) and
then write
 your create statements. Don't forget to write the parent property in. Use
 the same  name for the OnClick. Then delete the original component.

ie
implementation

{$R *.DFM}
var
  Button1: TButton;
{this will be put in automatically when you click the
event handler}
procedure TForm1.Button1Click(Sender: TObject);
begin
{put the extra begin and end in to prevent D3 getting rid of the code}
begin
 Canvas.TextOut(100,100,'Hey there''s my button');
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Button1:= TButton.Create(Self);
  with Button1 do
    begin
      Parent:= Form1;
      Height:= 40;
      Width:= 100;
      Caption:= 'My Button';
      OnClick:= Button1Click;
    end;
end;

end.

Gordon

Re:OnClick Event with a run-time create button


 "Balet Charles-Henri" <char...@prolec.ch> writes:

Quote
>who can help me to assign an procedure to an Button create in Run Time with

>btn := TButton.Create(Self);
>btn.onclick := procedure xxx;

>the compiler return an error with incompatibility on TNotify Event and
>Procedure ?
WGabor writes:

Can anyone please tell me what this means:
type TMyType=procedure(var1:type1;var2:type2[;var3:type3...]) of object;
I know what type TMyType=procedure(var1:type1;var2:type2[;var3:type3...])
means, but "of object" makes no sense to me.

These things can be confusing, as there are several layers of abstraction
involved.
TNotifyEvent is declared as follows:

type TNotifyEvent = procedure (Sender: TObject) of object;

This is used for On*** event properties which are essentially method pointers.
What that means in practice is that one routine delegates the execution of some
code to another. Let's look at an example from the ground up taking TControl as
our starting point and its OnClick behavior:

When the user clicks the left mouse button on the control, TControl traps this
message, and after letting it proceed to the operating system we see:

if PtInRect(...) then Click

Now, Click is another method of TControl. What do we see in Click?....

if Assigned(FOnClick) then FOnClick(Self);

FOnClick is the private field of TControl's OnClick event property.
What does this line of code mean? Well, what this says is that if my FOnClick
property (method/procedure  pointer) is pointing at something then execute that
method/procedure and tell it that it was I that told you to do it. Now FOnClick
is the private field that is published as:

property OnClick: TNotifyEvent read FOnClick write FOnClick;

This is what we see and use as component consumers! In practice what that
amounts to is that we double-click on the OnClick property in the Object
Inspector and an event handler is automatically created. This then is that
*something* already referred to - in other words, the code contained in this
handler will be executed after the left mouse button has come up. So we now
have yet another layer of abstraction - we're not interested in the fact that
the handler is called so much as what the handler contains! But don't forget,
TControl went in to this on the basis that the code could be executed but with
the proviso *and tell it that it was I that told you to do it*
So the handler must be able to accept a reference to TControl or to put it
another way, the method handler must be assignment-compatible with the method
pointer.
So anything that is assigned to OnClick must be declared as:

procedure TSomeClass.ProcedureName(Sender: TObject);
begin
  //code I want to execute
end;

I hope that answers both your questions

Charles Johnson

Re:OnClick Event with a run-time create button


In article <6pa4k9$dn...@bw107zhb.bluewin.ch>, "Balet Charles-Henri"

Quote
<char...@prolec.ch> writes:
>who can help me to assign an procedure to an Button create in Run Time with

>btn := TButton.Create(Self);
>btn.onclick := procedure xxx;

>the compiler return an error with incompatibility on TNotify Event and
>Procedure ?

1 That event procedure must have a parameter of (Sender : TObject).
2 It must be a method - ie defined as TSomeObjectType.OnSomeEvent(Sender :
TObject);

If you look up TNotifyEvent (which is the type of the event handling method )
it is defined as :-
TNotifyEvent = procedure (Sender: TObject) of object;

. . .  which is a succint way of saying what is in 1 and 2 above.

So write a method :-

procedure TForm.WhatIDoWhenAButtonIsClicked(Sender : TObject);
begin
 . . . your code - and Sender is the button
end;

put . . .

procedure WhatIDoWhenAButtonIsClicked(Sender : TObject);

 . . . immediately before the private clause in the interface section of your
unit.

then code . . .

Btn.OnClick := WhatIDoWhenAButtonIsClicked;

 . . . that's it

You can call the procedure what you like but it's best to call it
somethingOnClick.

Alan Lloyd
alangll...@aol.com

Re:OnClick Event with a run-time create button


the "Of Object" part just lets the compiler know that the procedure/function
will/can be called as part of a classes event handler ie and OnClick event.

-apm

Quote
>WGabor writes:

>Can anyone please tell me what this means:
>type TMyType=procedure(var1:type1;var2:type2[;var3:type3...]) of object;
>I know what type TMyType=procedure(var1:type1;var2:type2[;var3:type3...])
>means, but "of object" makes no sense to me.

Other Threads