RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TList.Remove Method

Deletes the first reference to the Item parameter from the Items array.

Pascal
function Remove(Item: Pointer): Integer;
C++
__fastcall int Remove(void * Item);

Call Remove to remove a specific item from the Items array when its index is unknown. The value returned is the index of the item in the Items array before it was removed. After an item is removed, all the items that follow it are moved up in index position and the Count is reduced by one. 

If the Items array contains more than one copy of the pointer, only the first copy is deleted.  

C++ Examples: 

 

/*
The following code adds an object to MyList if it isn’t
already in the list.
*/

#include <memory>       //for STL auto_ptr class

typedef struct AList
{
  int I;
  char C;
} TAList;

typedef AList* PAList;

TList *MyList;
PAList ARecord1, ARecord2;

void __fastcall DisplayTList(TList *theList)
{
  PAList AStruct;
  // Go through the list, writing the elements to the
  // canvas of a paintbox component.
  int Y = 10; // position on canvas
  for (int i = 0; i < theList->Count; i++)
  {
    AStruct = (PAList) theList->Items[i];
    Form1->PaintBox1->Canvas->TextOut(10, Y, IntToStr(AStruct->I));
    Y += 30;  // Increment Y Value again
    Form1->PaintBox1->Canvas->TextOut(10, Y, AStruct->C);
    Y += 30;  //Increment Y Value
  }
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  if (MyList->IndexOf(ARecord1) == -1) MyList->Add(ARecord1);
  DisplayTList(MyList);
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
  if (MyList->IndexOf(ARecord2) == -1) MyList->Add(ARecord2);
  DisplayTList(MyList);
}

void __fastcall TForm1::Button3Click(TObject *Sender)
{
  if (MyList->Count != 0)
  {
    for (int B = MyList->Count - 1; B >= 0; B--)
    {
      PAList ARecord;
      ARecord = PAList(MyList->Items[B]);
      MyList->Remove(ARecord);
    }
  }
  Form1->PaintBox1->Repaint();
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  static std::auto_ptr<TList> _MyListCleaner(MyList = new TList);
  static std::auto_ptr<TAList> _ARecord1Cleaner(ARecord1 = new TAList);
  ARecord1->I = 100;
  ARecord1->C = 'Z';
  static std::auto_ptr<TAList> _ARecord2Cleaner(ARecord2 = new TAList);
  ARecord2->I = 200;
  ARecord2->C = 'X';
}
/*
The following code adds a new object to a list in a list
object and then removes it:
*/
#include <memory>       //for STL auto_ptr class

class TMyClass : public TComponent
{
__published:    // IDE-managed Components
private:    // User declarations
public:     // User declarations
  AnsiString MyString;
    __fastcall TMyClass(TComponent* Owner, AnsiString mystr);
};

__fastcall TMyClass::TMyClass(TComponent* Owner, AnsiString mystr)
    : TComponent(Owner)
{
  MyString = mystr;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  std::auto_ptr<TList> list(new TList);
  TMyClass *TheObject = new TMyClass(Form1, "This is an object."); // The owner will clean this up;
  list->Add(TheObject); // add AnsiString instance to list
  MessageDlg("The list has " + IntToStr(list->Count) + " objects",
    mtInformation, TMsgDlgButtons() << mbOK, 0);
  list->Remove(TheObject);
  MessageDlg("The list has " + IntToStr(list->Count) + " objects",
    mtInformation, TMsgDlgButtons() << mbOK, 0);
}

 

Delphi Examples: 

{
The following code adds an object to MyList if it isn’t
already in the list.
}
type
  PMyList = ^AList;
  AList = record
    I: Integer;
    C: Char;
  end;
var
  Form1: TForm1;
  MyList: TList;
  ARecord1, ARecord2: PMyList;

implementation

{$R *.dfm}

procedure DisplayTList(TheList: TList);
var
  ARecord: PMyList;
  B: Byte;
  Y: Word;
begin
    { Now paint the items onto the paintbox}
    Y := 10;             {Variable used in TextOut function}
    for B := 0 to (TheList.Count - 1) do
    begin
      ARecord := TheList.Items[B];
      Form1.PaintBox1.Canvas.TextOut(10, Y, IntToStr(ARecord^.I)); {Display I}
      Y := Y + 30;  {Increment Y Value again}
      Form1.PaintBox1.Canvas.TextOut(10, Y, ARecord^.C);  {Display C}
      Y := Y + 30;  {Increment Y Value}
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if (MyList.IndexOf(ARecord1) = -1) then MyList.Add(ARecord1);
  DisplayTList(MyList);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if (MyList.IndexOf(ARecord2) = -1) then MyList.Add(ARecord2);
  DisplayTList(MyList);
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  ARecord: PMyList;
  B: Integer;
begin
  if (MyList.Count <> 0) then
  begin
    for B := (MyList.Count - 1) downto 0 do
    begin
      ARecord := MyList.Items[B];
      MyList.Remove(ARecord);
    end;
  end;
  Form1.PaintBox1.Repaint;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyList := TList.Create;
  New(ARecord1);
  ARecord1^.I := 100;
  ARecord1^.C := 'Z';
  New(ARecord2);
  ARecord2^.I := 200;
  ARecord2^.C := 'X';
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Dispose(ARecord1);
  Dispose(ARecord2);
  MyList.Free;
end;
{
The following code adds a new object to a list in a list
object and then removes it:
} 
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  TMyClass = class
    MyString: string;
    constructor Create(S: string);
  end;

var
  Form1: TForm1;
  MyList: TList;

implementation

{$R *.dfm}

constructor TMyClass.Create(S: string);
begin
  MyString := S;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  MyObject: TMyClass;
begin
  MessageDlg('The list starts with ' + IntToStr(MyList.Count) + ' objects',
    mtInformation, [mbOk], 0);
  try
    MyObject := TMyClass.Create('Semper Fidelis!');  { create a class instance }
    try
      MyList.Add(MyObject);      { add instance to list }
      MessageDlg('The list has ' + IntToStr(MyList.Count) + ' objects',
                 mtInformation, [mbOk], 0);
      MyList.Remove(MyObject);
      MessageDlg('The list has ' + IntToStr(MyList.Count) + ' objects', 
                 mtInformation, [mbOk], 0);
    finally
      MyObject.Free;
    end;                  { don't forget to clean up! }
  finally
    MyList.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  MyList := TList.Create;              {Create a list }
  MyList.Add(PChar('A string')); {Add a string}
  MyList.Add(PChar('')); { Add an empty string }
  MyList.Add(PChar('A third string')); {Add a string}
  MyList.Add(nil);              {Add nil }
  MyList.Add(PChar('A fifth string')); {Add a string}
  MyList.Add(PChar('')); { Add another empty string }
  MyList.Add(PChar('A seventh string')); {Add a string}
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!