Solved

Add, Change and Deleting Itemns or Columns on TListView

Posted on 2006-07-15
11
1,405 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

12 Experts available now in Live!

Get 1:1 Help Now