Board index » delphi » Disable/Enable event handlers in runtime

Disable/Enable event handlers in runtime

Hi all,

I want to disable certain event handlers of a component at runtime when
I don't need them and disable them when I need them. How to do it?

Thanks in advance.

Iago.

Sent via Deja.com http://www.deja.com/
Before you buy.

 

Re:Disable/Enable event handlers in runtime


Quote
iag...@my-deja.com wrote:

> Hi all,

> I want to disable certain event handlers of a component at runtime when
> I don't need them and disable them when I need them. How to do it?

easy.

to disable:

MyButton.OnClick:=nil;

to re-enable:

MyButton.OnClick:=Form1.ButtonClick;

Is this what you meant?

MH.

--
Martin Harvey. mar...@pergolesi.demon.co.uk
     http://www.pergolesi.demon.co.uk

Re:Disable/Enable event handlers in runtime


Quote
iag...@my-deja.com wrote:
>Hi all,

>I want to disable certain event handlers of a component at runtime when
>I don't need them and disable them when I need them. How to do it?

>Thanks in advance.

Once you defined or created an event handler you can do this:

if NeedIt then
        Form1.OnKeyPress := Form1KeyPress
else
        Form1.OnKeyPress := nil;

Smola
--
My ass is on fire

Re:Disable/Enable event handlers in runtime


Quote
On Wed, 24 May 2000 22:15:14 GMT, iag...@my-deja.com wrote:
>Hi all,

>I want to disable certain event handlers of a component at runtime when
>I don't need them and disable them when I need them. How to do it?

procedure TSomeObject.SomeProc;
var
  aProc: TNotifyEvent;
begin
  aProc := OnChange;
  OnChange := nil;
  try
    ...
  finally
    OnChange := aProc;
  end;
end;

Re:Disable/Enable event handlers in runtime


Quote
In article <392c6924.7639...@news.tel.hr>, asmol...@foi.hr (Smola) writes:
>iag...@my-deja.com wrote:

>>Hi all,

>>I want to disable certain event handlers of a component at runtime when
>>I don't need them and disable them when I need them. How to do it?

>>Thanks in advance.

>Once you defined or created an event handler you can do this:

>if NeedIt then
>    Form1.OnKeyPress := Form1KeyPress
>else
>    Form1.OnKeyPress := nil;

This shows the principal of doing it, but because the disabling is often
separated from the initial nominating, and as such disabling is usually
temporary while other code is executed, it is better to store the old event
handler and replace it from the stored value.

var
  OldOnKeyPress : TKeyPressEvent;

  OldOnKeyPress := Self.OnKeyPress;
  Self.OnKeyPress := nil;
//
//. . . other code needing a nil event handler
//
  Self.OnKeyPress := OldOnKeyPress;

One less thing to remember when you modify your code six-months hence <g>.

Alan Lloyd
alangll...@aol.com

Re:Disable/Enable event handlers in runtime


Quote
<iag...@my-deja.com> wrote in message news:8ghk97$1p4$1@nnrp1.deja.com...
> Hi all,

> I want to disable certain event handlers of a component at runtime when
> I don't need them and disable them when I need them. How to do it?

> Thanks in advance.

A lot of posts have shown how to disable event handlers by setting the event
to nil. In most circumstances this works well. However there may be cases
where it makes more sense to use a boolean and test it in the handler. It
makes for less efficient code, but I often use it because I think that it
makes the code clearer and easier to debug. An extension of this concept is
to use an integer to control execution. This lets different areas of the
code disable/enable a handler without having to worry about who else might
need it disabled, e.g.

. . .
    private
        ClickDisableCount : integer;
. . .
    end;

procedure tForm1.AControlClick (Sender : tObject);

begin
if ClickDisableCount <= 0
then begin
       end;
end;

// to disable the click
inc (ClickDisableCount);

// to enable the click (if no one else has it disabled)
dec (ClickDisableCount);

Quote

> Iago.

> Sent via Deja.com http://www.deja.com/
> Before you buy.

Other Threads