如何以编程方式添加ListBoxItem?

我只能通过单击组件 – > 项目编辑器找到如何创建ListBoxItem

我们如何使用Firemonkey编程方式创建ListBoxItem

假设ListBoxItem是名为ListBox1的现有TListBox组件的项,则可以像这样添加项:

 ListBox1.Items.Add('an item name'); 

替代:

 var id: Integer; . . . ListBox1.Items.AddObject('an item name', TObject(id)); 

编辑请注意,只有在未对基础列表进行排序时,才必须认为此方法有效。

只需创建列表框项,然后将其添加到列表框中:

 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; 

您还可以使用:

 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;