How can I add a ListBoxItem programmatically?

雨燕双飞 提交于 2019-12-08 10:40:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!