3 9 Votes

Delphi/Lazarus: Add Item or Line to ListView

Info by Delphian | 09/06/2013 at 18:35

If you would like to add an item to a ListBox in Delphi or Lazarus, you can simply use the function Add for this:

ListBox1.Items.Add('This text will be added.');

The procedure is a little bit more complicated when trying to add a new item or a new line to a ListView, because each line of a TListView can consist of multiple items (which are the columns).

Each line consists of a TListItem, which we have to create and add to our ListView individually:

var
  li: TListItem;
begin
  li:=ListView1.Items.Add;
  li.Caption:='First Column';
  li.SubItems.Add('Second Column');
  li.SubItems.Add('Third Column');
end;

First of all, we set the item "li" to a new item of our ListView. After that, we set the caption (which is displayed as first column) and add as much subitems as we like (which are the following columns).

Alternative Writing

If we do not want to declare the variable TListItem seperately, we can also proceed as follows:

with ListView1.Items.Add do begin
   Caption:='First Column';
   SubItems.Add('Second Column');
   SubItems.Add('Thid Column');
end;

This has the same effect like our first example.

Add multiple Items

If we want to add several items at the same time (for example within a loop, we can simply repeat one of the described procedures as often as necessary.

ReplyPositiveNegative

About the Author

Avatar AuthorThe author has not added a short description to his profile yet.
Show Profile | Message

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own articles on askingbox.com. How to do.