TStringList.OnChange

From Soldat Community Wiki
Jump to: navigation, search
property OnChange: TNotifyEvent
 Access mode: RW

Description

OnChange can be assigned to respond to changes that have occurred in the list. The handler is called whenever strings are added, moved, modified or deleted from the list.

The OnChange event is triggered after the modification took place. When the modification is about to happen, an OnChanging event occurs.

Example

Following example tracks changes made to the list.

var
  s: TStringList;
  Count1,Count2: integer;

procedure Before(Sender: TObject);
begin
  Count1 := TStringList(Sender).Count;
end;

procedure After(Sender: TObject);
begin
  Count2 := TStringList(Sender).Count;
  
  if Count2 > Count1 then
    WriteLn('String added')
  else if Count2 < Count1 then
    WriteLn('String removed')
  else
    WriteLn('Content modified or moved');
end;  
  
begin
  s := File.CreateStringList();

  s.OnChanging := @Before;
  s.OnChange := @After;
  
  s.Add('a');   // String added
  s.Add('b');   // String added
  s.Delete(1);  // String removed
  s[0] := 'a2'; // String modified or moved
  
  s.Free;
end.