2004. július 11., vasárnap

How to set all events of an object to NIL at runtime (2)


Problem/Question/Abstract:

I need to change the 'OnChange' event of all of my components. Can someone give me a starting point as to how I can go about changing this event via RTTI. I want to be able to pass an event name or set the event to nil.

Answer:

uses
  TypInfo;

procedure TFrmRTTIOnChange.Button1Click(Sender: TObject);
var
  propInfo: PPropInfo;
  thisEvent: TNotifyEvent;
begin
  propInfo := GetPropInfo(Memo1.ClassInfo, 'OnChange');
  if propInfo <> nil then
  begin
    thisEvent := Memo1AltChange;
    SetOrdProp(Memo1, PropInfo, integer(@thisEvent));
  end;
end;

procedure TFrmRTTIOnChange.Memo1Change(Sender: TObject);
begin
  Caption := 'Normal On Change';
end;

procedure TFrmRTTIOnChange.Memo1AltChange(Sender: TObject);
begin
  Caption := 'Alternate On Change';
end;

Nincsenek megjegyzések:

Megjegyzés küldése