Re: Delphi TStringList not allowing duplicatesPosted by webmaster Guido on February 05, 2002 at 19:14:48: In Reply to: Delphi TStringList not allowing duplicates posted by BeckyR on February 05, 2002 at 15:22:02:
: How do I use the duplicates property to raise an exception when a duplicate string is
added to a Delphi stringlist? : for iTemp := 0 to frmData.sgClasses.Rowcount - 1 do ------------------------------------ For an exception to be raised when you add a duplicate, you should set two properties
of the Delphi stringlist: In the following example, all the animals are added to the stringlist, except for the second 'cat': procedure TForm1.Button1Click(Sender: TObject);
const
Animals: array[1..4] of string = ('cat', 'dog', 'cat', 'bird');
var
SL: TStringList;
i: integer;
begin
SL := TStringList.Create;
SL.Sorted := True;
SL.Duplicates := dupError;
for i := 1 to 4 do
try
SL.Add(Animals[i]);
except
on EStringListError do
Showmessage('Duplicate at ' + IntToStr(i) + ': ' + Animals[i]);
end;
SL.Free;
end;
|
|