Solved

Add, Change and Deleting Itemns or Columns on TListView

Posted on 2006-07-15
11
1,463 Views
Last Modified: 2011-10-03
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;


0
Comment
Question by:rafaelrgl
  • 9
  • 2
11 Comments
 
LVL 9

Expert Comment

by:ginsonic
ID: 17116535
var itm:TListItem;

to add items:


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

to add subitems:

itm.SubItems.Add('SubItem1');
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 17116550
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]:='';
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 17116556
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;
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 9

Expert Comment

by:ginsonic
ID: 17116558
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.
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 17117351
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
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 17122549
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;
0
 
LVL 9

Accepted Solution

by:
ginsonic earned 500 total points
ID: 17122603
For subitems adding can use:

ListView1.Items.Item[0].SubItems.Add('SubItem1');

But I preffer next way ( the new item is added at the end of items list and not inserted somewhere inside the list ):

procedure TForm1.Button1Click(Sender: TObject);
var itm:TListItem;
begin
  itm:=ListView1.Items.Add;
  itm.Caption:='Item1';
  itm.SubItems.Add('SubItem1');
end;
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 17131396
any comment
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 17138542
Have my comments solve your problems?
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 17140617
yes, thank you so much.
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 17143675
My pleasure!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Simple Delphi Question 9 88
Delphi: how to implement a User Shortcut mapper? 1 107
Dev express lookupcombo 3 26
can i put an image from resource into image tag ? 6 24
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now