'How can I add a ListBoxItem programmatically?
I could only find how to create a ListBoxItem
by clicking on the component -> Items Editor.
How can we create programmatically a ListBoxItem
using Firemonkey
?
Solution 1:[1]
Assuming that a ListBoxItem
is an item of an existing TListBox
component named ListBox1
, the item can be added like this:
ListBox1.Items.Add('an item name');
an alternative:
var
id: Integer;
. . .
ListBox1.Items.AddObject('an item name', TObject(id));
EDIT Notice that this approach has to be considered valid only if the underlying list is not sorted.
Solution 2:[2]
Simply create the list box item, and add it to the list box:
var
ListBoxItem: TListBoxItem;
begin
ListBoxItem := TListBoxItem.Create(ListBox1);
ListBoxItem.Text := 'foo';
// set other properties of the list box item at this point
ListBox1.AddObject(ListBoxItem);
end;
Solution 3:[3]
I created ListBoxItem inside my ComboBox with some extra details:
var
LBoxItem : Array [1..50] of TListBoxItem;
procedure TForm1.Button2Click(Sender: TObject);
var
i: Integer;
begin
for i := 1 to 10 do
begin
LBoxItem[i] := TListBoxItem.Create(ComboBox2);
LBoxItem[i].Parent := ComboBox2;
LBoxItem[i].Text := 'LBox'+IntToStr(i);
LBoxItem[i].Font.Size := 18;
LBoxItem[i].StyledSettings := [];
LBoxItem[i].Height := 20;
end;
end;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | David Heffernan |
Solution 3 | Wellington Telles Cunha |