?
Solved

Add, Change and Deleting Itemns or Columns on TListView

Posted on 2006-07-15
11
Medium Priority
?
1,744 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

771 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