Link to home
Start Free TrialLog in
Avatar of rafaelrgl
rafaelrgl

asked on

Add, Change and Deleting Itemns or Columns on TListView

How Can I do this command's on TListView

This is how I insert a Item:

     ListView1.Items.Insert(conaux.ThreadID).Caption := ConAux.IP;


How can I do the rest:

delete or change item;
Insert, delete or change column;
Insert, delete or change subitems;


Avatar of ginsonic
ginsonic
Flag of Romania image

var itm:TListItem;

to add items:


itm:=ListView1.Items.Add;
itm.Caption:=ConAux.IP

to add subitems:

itm.SubItems.Add('SubItem1');
To delete a item:

ListView.Items.Item[0].Delete;

Where 0 is your wished item index.

You can't delete the subitem but can set to '' the Caption. When delete the item the subitems gone too :)

To cange an item:

ListView1.Items.Item[0].Caption := 'Item2';

or a subitem:

ListView1.Items.Item[0].SubItems[0]:='';
Add column:

ListView1.Columns.Add;
ListView1.Column[ListView1.Columns.Count-1].Caption:='Test';

Change column:

ListView1.Column[MyColumnIndex].Caption:='Test 2';

Delete a column:

ListView1.Column[MyColumnIndex].Destroy;
Sorry, I write that subitems can't be deleted. Not true, you can do :

ListView1.Items.Item[0].SubItems.Delete(0);

Change 0 with your indexes.
Avatar of rafaelrgl
rafaelrgl

ASKER

insert   item        :        ListView1.Items.Insert(conaux.ThreadID).Caption := ConAux.IP;
delete  item        :        ListView.Items.Item[0].Delete;
change item        :       ListView1.Items.Item[0].Caption := 'Item2';
insert   subitem   :       ListView1. ?????????????????????????????????????
delete  subitem    :       ListView1.Items.Item[0].SubItems.Delete(0);
change subitem   :        ListView1.Items.Item[0].SubItems[0]:='';

insert   column    :       ListView1.Columns.Add;
                                  ListView1.Column[ListView1.Columns.Count-1].Caption:='Test';

delete   column    :       ListView1.Column[MyColumnIndex].Destroy;
change column    :       ListView1.Column[MyColumnIndex].Caption:='Test 2';

question:

how can i change the index 0 to a function that find the index using the caption
Not my code. Found sometime ago on net:

function FindListviewItem( lv: TListview; const S: String; Column: Integer ): TListItem;
var
  i: Integer;
  found: Boolean;
begin
  Assert( Assigned( lv ));
  Assert( (lv.viewstyle = vsReport) or (column = 0) );
  Assert( S <> '' );
  for i := 0 To lv.items.count - 1 do
  begin
    result := lv.Items[i];
    if column = 0 then
      found := AnsiCompareText( result.caption, S ) = 0
    else
      if column <= result.subitems.count then
        found := AnsiCompareText( result.subitems[column - 1], S ) = 0
      else
        found := false;
    if found Then
      Exit;
  end;

  Result := nil;
end;


procedure TForm1.Button2Click(Sender: TObject);
var i:integer;
begin
  i:=ListView1.Items.IndexOf(FindListviewItem( ListView1, 'Item2', 0 ));
  ShowMessage(IntToStr(i));
end;
ASKER CERTIFIED SOLUTION
Avatar of ginsonic
ginsonic
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
any comment
Have my comments solve your problems?
yes, thank you so much.
My pleasure!