Freddie Bell
Freddie Bell

Reputation: 2287

Is there a quick way to disable/enable all event handlers

Typically in my applications, I have a place where I "load up" an in-memory dataset (TClientDataSet or DevEx TdxMemData). Before the load it, I do something like this:

CDS1.BeforeUpdate := nil;
CDS1.AfterUpdate := nil;
CDS1.AfterPost := nil;

... and so on. Then after the load (eg. LoadData(CDS1))

CDS1.BeforeUpdate := CDS1BeforeUpdate;
CDS1.AfterUpdate := CDSAfterUpdate;
CDS1.AfterPost := CDS1AfterPost;

It's very tedious, especially because not all event handlers are assiged in every context in every application, and sometimes I miss one that comes back to bite me at runtime.

Wouldn't it be nice if I could just call

CDS1.DisableallEventHandlers;
LoadData(CDS1);
CDS1.EnableallEventHandlers; // which were assigned at design-time, that is

Upvotes: 1

Views: 1078

Answers (1)

ViRuSTriNiTy
ViRuSTriNiTy

Reputation: 5155

I recommend creating a class helper with the methods DisableallEventHandlers and EnableallEventHandlers whereas each method uses RTTI to enumerate over the event handler properties and stores / retrieves the event handler pointers to / from a map.

Upvotes: 1

Related Questions