List Info

Thread: Creating Events for new component




Creating Events for new component
user name
2007-01-28 10:37:01

Hello all:

I am making something that should be really easy into something really
difficult;

I have created a composite control, containing an edit box and several
speed buttons. One of the speed buttons, on the click event, I would
like to substitute a new event for the TNotifyEvent that normally occurs.

The original code using the default handler looks like this:
------------------
Function GetBtn1Click: TNotifyEvent;
Procedure SetBtn1Click(const E: TNotifyEvent);

property OnBtn1Click: TNotifyEvent read GetBtn1Click write SetBtn1Click;

Function TjmEdit.GetBtn1Click: TNotifyEvent;
Begin
Result := FBtn1.OnClick;
End;
Function TjmEdit.SetBtn1Click(const E: TNotifyEvent)
Begin
FBtn1.OnClick := E;
End;
-------------------
This works - when I create a handler to deal with the click it works fine.

Here is what I want, however. I would like to create this component
such that it will allow me to attach a different event handler
signature. For example, I want to define my new event as follows:

TjmFileRefBtnPressedEvent = procedure(Sender: TObject; var FileRec:
Integer; var FileName: String) of object;

I would like to assign this to the button click instead of the
TNotifyEvent (the compiler absolutely hates this, since the procedure
signature is different.)

What kind of magic do I need to do to get this to work? Ideally,
inside my component, some kind of code will act as a moderator
between the original TSpeedButton.OnClick event and the new
TjmFileRefBtnPressedEvent. If no event handler is assigned, pressing
the button would do nothing. If I assign a handler, such as

Procedure WorkWithFileRepository(Sender: TObject; Var Key: Integer;
Var Fil: String);

then pressing the button would execute this method.

Thank
jamie

__._,_.___
.

__,_._,___
Re: Creating Events for new component
user name
2007-01-28 10:57:21


All you need to do is use that external condition to
decide in the TNotify event whether to execute the
code or not in a decision branch (a lot of programmers
use the Tag property for this):

For example: (some external event sets the tag
property of the speedbutton)

procedure TForm.SpeedbuttonClick(Sender: TObject);
begin
If Speedbutton.tag = 0 Then Exit
else If Speedbutton.tag = 1 Then
begin
//code that is in the first TNotify event
end
else If Speedbutton.tag = 2 Then
begin
//code that is in 2nd TNotifyEvent
end;
end;

from what you've explained, I see no reason to be
swapping TNotify events in and out (although it's
certainly doable).

Dave

--- "Jamie L. Mitchell&quot; < jamiemi%40frontiernet.net">jamiemifrontiernet.net>
wrote:

> Hello all:
>;
> I am making something that should be really easy
>; into something really
&gt; difficult;
>
> I have created a composite control, containing an
> edit box and several
> speed buttons. One of the speed buttons, on the
> click event, I would
&gt; like to substitute a new event for the TNotifyEvent
> that normally occurs.
>
> The original code using the default handler looks
&gt; like this:
&gt; ------------------
> Function GetBtn1Click: TNotifyEvent;
> Procedure SetBtn1Click(const E: TNotifyEvent);
>;
> property OnBtn1Click: TNotifyEvent read GetBtn1Click
> write SetBtn1Click;
>
> Function TjmEdit.GetBtn1Click: TNotifyEvent;
> Begin
&gt; Result := FBtn1.OnClick;
> End;
>; Function TjmEdit.SetBtn1Click(const E: TNotifyEvent)
> Begin
&gt; FBtn1.OnClick := E;
> End;
>; -------------------
> This works - when I create a handler to deal with
>; the click it works fine.
&gt;
> Here is what I want, however. I would like to
> create this component
> such that it will allow me to attach a different
> event handler
> signature. For example, I want to define my new
> event as follows:
>
> TjmFileRefBtnPressedEvent = procedure(Sender:
&gt; TObject; var FileRec:
> Integer; var FileName:
> String) of object;
>
> I would like to assign this to the button click
&gt; instead of the
> TNotifyEvent (the compiler absolutely hates this,
&gt; since the procedure
> signature is different.)
>
> What kind of magic do I need to do to get this to
> work? Ideally,
> inside my component, some kind of code will act as a
> moderator
> between the original TSpeedButton.OnClick event and
> the new
> TjmFileRefBtnPressedEvent. If no event handler is
> assigned, pressing
> the button would do nothing. If I assign a handler,
> such as
>
> Procedure WorkWithFileRepository(Sender: TObject;
> Var Key: Integer;
> Var Fil: String);
>
> then pressing the button would execute this method.
>
> Thank
&gt; jamie
&gt;
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

__________________________________________________________
We won't tell. Get more on shows you hate to love
(and love to hate): Yahoo! TV's Guilty Pleasures list.
http://tv.yahoo.com/collections/265

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )