410 Votes

Delphi/Lazarus: Add Item or Line to ListView

Info by Delphian | Last update on 2020-11-01 | Created on 2013-06-09

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 complex 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 separately, 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 as 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

AvatarThe author has not added a profile short description yet.
Show Profile

 

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 article on askingbox.com. That’s how it’s done.