Search code examples
delphidelphi-11-alexandria

How to trigger all onChange events of all TMemo components in a form


I want to trigger all onChange events of all TMemo components in a form, when the form is shown.

For this i'm using the code below :

var
  i: integer;
  m: tmemo;
begin
  for i := 0 to componentcount - 1 do
  begin
    if components[i] is tmemo then
    begin
      m := components[i] as tmemo;
      m.onchange(m);
    end;
  end;
end;

Problem is, i'm getting some Access Violations on the loop above, probably due the fact some memos don't have an onChange event.

How can i test if the event exists, before trigger it on the line below ?

m.onchange(m);

Solution

  • You need to check if an event handler is assigned before calling it :

    if Assigned(m.OnChange) then
      m.OnChange(m);