TListBox Items AddObject Firemonkey 10.2 [duplicate]

China☆狼群 提交于 2019-12-13 04:54:40

问题


I'm try on formShow add

ListBox1.Items.AddObject('TEST 1', TObject(1)) ;
ListBox1.Items.AddObject('TEST 2', TObject(2)) ;

but app automatically close (crash).

This example work fine

ListBox1.Items.Add('TEST 1');
ListBox1.Items.Add('TEST 2');

Any solution how use Items.AddObject?


回答1:


For FMX TListBox I suggest you use the Tag property instead.

aItem: TListBoxItem;
begin
   aItem := TListBoxItem.Create(Self);
   aItem.Text := 'TEST 1';
   aItem.Tag := 1;
   aItem.Parent := ListBox1;

   aItem := TListBoxItem.Create(Self);
   aItem.Text := 'TEST 2';
   aItem.Tag := 2;
   aItem.Parent := ListBox1;
end

This is just a pseudo-code, but you get the idea. It also gives you the ability to derive a class from TListBoxItem and make it do something that normal TListBoxItem will not do or have different class for different items.



来源:https://stackoverflow.com/questions/54927994/tlistbox-items-addobject-firemonkey-10-2

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