RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TCustomListView.Selected Property

Indicates the first selected item in the list view.

Pascal
property Selected: TListItem;
C++
__property TListItem Selected;

Read Selected to access the properties of the first selected item in the list. If SelCount is 0, Selected is nil (Delphi) or NULL (C++). If SelCount is greater than 1, subsequent selected items can be located by checking the Selected property of the items found using the GetNextItem method.  

Set the Selected property to select an item in the list. If MultiSelect is true, setting Selected adds an item to the selection. If MultiSelect is false, setting Selected changes the selection. Setting Selected to nil (Delphi) or NULL (C++) deselects all items in the list. 

When an item is selected, the OnChanging and OnChange events are generated.  

C++ Examples: 

 

/*
The following example inserts a list item after 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 after 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) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!