问题
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
?
回答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.
回答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;
回答3:
You can also use:
var
aItem: TListViewItem;
begin
for i := 0 to 10 do
begin
aItem := ListView1.Items.Add;
aItem.Text := 'Item Text';
// you can set properties in here..
end;
end;
来源:https://stackoverflow.com/questions/33284499/how-can-i-add-a-listboxitem-programmatically