TStringList.OnChanging

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

Description

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

The OnChanging event is triggered before the modification will take place. When the modification has happened, an OnChange 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.