Say I have a Button1.OnClick event linked to Button1Click procedure. I also have Button2.OnClick linked to some other procedure. How do I check that both events are linked t
I know this is an old question...but here's my 2cents...
This is answer is similar to Nat's but doesn't limit us to only TNotifyEvents...and answers David's question of how to do this with out it being a hack...
function CompareMethods(aMethod1, aMethod2: TMethod): boolean;
begin
Result := (aMethod1.Code = aMethod2.Code) and
(aMethod1.Data = aMethod2.Data);
end;
I use it like so
procedure TDefaultLoop.RemoveObserver(aObserver: TObject; aEvent: TNotifyEvent);
var
a_Index: integer;
begin
for a_Index := 0 to FNotifyList.Count - 1 do
if Assigned(FNotifyList[a_Index]) and
(TNotify(FNotifyList[a_Index]).Observer = aObserver) and
CompareMethods(TMethod(TNotify(FNotifyList[a_Index]).Event), TMethod(aEvent)) then
begin
FNotifyList.Delete(a_Index);
FNotifyList[a_Index] := nil;
end;
Also a quick and dirty demonstration
procedure TForm53.Button1Click(Sender: TObject);
var
a_Event1, a_Event2: TMethod;
begin
if Sender is TButton then
begin
a_Event1 := TMethod(Button1.OnClick);
a_Event2 := TMethod(Button2.OnClick);
if CompareMethods(TMethod(TButton(Sender).OnClick), a_Event1) then
ShowMessage('Button1Click Same Method');
if CompareMethods(TMethod(TButton(Sender).OnClick), a_Event2) then
ShowMessage('Button2Click Same Method');
end;
end;
procedure TForm53.Button2Click(Sender: TObject);
var
a_Event1, a_Event2: TMethod;
begin
if Sender is TButton then
begin
a_Event1 := TMethod(Button1.OnClick);
a_Event2 := TMethod(Button2.OnClick);
if CompareMethods(TMethod(TButton(Sender).OnClick), a_Event1) then
ShowMessage('Button1Click Same Method');
if CompareMethods(TMethod(TButton(Sender).OnClick), a_Event2) then
ShowMessage('Button2Click Same Method');
end;
end;