Search code examples
delphidelphi-11-alexandria

How to call onChange event using FindComponent?


If I'm searching a component like this :

TMemo(FindComponent('mymemoname'));

How do I call the OnChange event of this TMemo? The example below won't work :

TMemo(FindComponent('mymemoname')).Change();

Thanks


Solution

  • The Change() method is protected, so to call it directly like you are trying to, you would need to use an accessor class to grant access to the calling scope, eg:

    type
      TMemoAccess = class(TMemo)
      end;
    
    TMemoAccess(TMemo(FindComponent('mymemoname'))).Change();
    
    

    Otherwise, you can just call the OnChange handler directly instead:

    var TheMemo := TMemo(FindComponent('mymemoname'));
    TheMemo.OnChange(TheMemo);