首页 > 解决方案 > TListBox 项目 AddObject Firemonkey 10.2

问题描述

我正在尝试 formShow 添加

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

但应用程序自动关闭(崩溃)。

这个例子工作正常

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

任何解决方案如何使用 Items.AddObject?

标签: delphifiremonkey

解决方案


因为FMX TListBox我建议您改用该Tag属性。

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

这只是一个伪代码,但你明白了。它还使您能够从中派生一个类,TListBoxItem并使其做一些正常TListBoxItem不会做的事情,或者对不同的项目有不同的类。


推荐阅读