RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TListItems.Insert Method

Creates a new list item and inserts it into the list view.

Pascal
function Insert(Index: Integer): TListItem;
C++
__fastcall TListItem Insert(int Index);

Call Insert to insert a new TListItem object into the list view at the specified Index. Insert returns the list item that is inserted.  

C++ Examples: 

 

/*
The following example inserts a list item before the selected
item in the list view.  Place a TButton, a TEdit and a
TListView in the form.
*/ 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TListItem *InsertItem;
  if (ListView1->Selected == NULL) return;
  InsertItem =
    ListView1->Items->Insert(ListView1->Selected->Index);
  InsertItem->Caption = Edit1->Text;
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  TListItem *ListItem;
  ListView1->ViewStyle = vsList;
  ListItem = ListView1->Items->Add();
  ListItem->Caption = "Apples";
  ListItem = ListView1->Items->Add();
  ListItem->Caption = "Oranges";
  ListItem = ListView1->Items->Add();
  ListItem->Caption = "Pears";
}

 

Delphi Examples: 

{
The following example inserts a list item before the selected
item in the list view.  Place a TButton, a TEdit and a
TListView in the form.
} 
procedure TForm1.Button1Click(Sender: TObject);
var InsertItem : TListItem;
begin
  with ListView1 do
  begin
    if (Selected = nil) then exit;
    InsertItem := Items.Insert(Selected.Index);
  end;
  InsertItem.Caption := Edit1.Text;
end;

procedure TForm1.FormCreate(Sender: TObject);
var ListItem : TListItem;
begin
  ListView1.ViewStyle := vsList;
  ListItem := ListView1.Items.Add;
  ListItem.Caption := 'Apples';
  ListItem := ListView1.Items.Add;
  ListItem.Caption := 'Oranges';
  ListItem := ListView1.Items.Add;
  ListItem.Caption := 'Pears';
end;

 

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