Search code examples
delphifiremonkey

TListBox Items AddObject Firemonkey 10.2


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?


Solution

  • 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.