Firemonkey ListBox Item with image

13,155

Your picture does not change because in your code you are only loading a single image. You also made a big mistake of constantly loading the image in a loop, you should only load the image once when the form is created and then simply reference the correct Bitmap.

if Odd(I) then
    Item.ItemData.Bitmap := Image1.Bitmap         
  else
    Item.ItemData.Bitmap := Image2.Bitmap; 

The irony is that most people forget to check the Samples folder, most usually located at C:\Users\Public\Documents\RAD Studio\XX.0\Samples\FireMonkey.

What you should do is take a close look at the CustomListBoxsample.

There is nothing wrong with following other tutorials but given how fast Firemonkey changes each version and the tutorial that you linked being quite old, I would suggest you fall back to the already included Delphi example, it's up to date, easy to understand and basically does exactly what you are looking for.

Share:
13,155

Related videos on Youtube

HolgerH
Author by

HolgerH

Updated on June 04, 2022

Comments

  • HolgerH
    HolgerH almost 2 years

    i made a Firemonkey custom ListBox Item, designed with a stylebook. When i try to insert ListBox items, everything works fine (insert text etc.) except to change the picture in the ListBox Item.

    I followed this tutorial on: http://www.experts-exchange.com.

    Here is my code:

    procedure TForm2.Button1Click(Sender: TObject);
    var
        i         : Integer;
        LBItem    : TListBoxItem;
        ItemImage : Timage;
    begin
        ListBox1.BeginUpdate;
        ListBox1.Items.Clear;
        try
            for i := 0 to 9 do begin
    
                LBItem := TListBoxItem.Create(nil);
                LBItem.Parent := ListBox1;
                LBItem.StyleLookup := 'rowLayout';
                LBItem.StylesData['textName'] := 'Some text...';
                LBItem.StylesData['textFormat'] := 'Some more text...';
    
                ItemImage := LBItem.FindStyleResource('picture') as TImage;
                if Assigned(ItemImage) then
                    LBItem.ItemData.Bitmap.LoadFromFile('D:\MyTestPicture.jpg');
            end;
        finally
            ListBox1.EndUpdate;
        end;
    end;
    

    "rowLayout" is my layout in the stylebook, i made for the ListBox Item. "textName" and "textFormat" are some TText, which i placed in the ListBox Item. "picture" is the TImage in my ListBox Item.

    What is going wrong with my code? I am working with Delphi XE4.

    Thanks for your help and best regards, Holger

  • HolgerH
    HolgerH almost 11 years
    Hi Peter, thanks for your answer! I taked a deep look at the Demo folder, but when i tried to implement it in my program, it dosent work with the image. But now, i played a bit with the "style Name". And i find out, when i use the style name: "icon" then does the following code work perfect: LBItem.ItemData.Bitmap.LoadFromFile(folder + '\' + TJSONPair(item).JsonValue.Value); When i changed the style name from my image to something else, it dosent work. Best regards, Holger
  • Machado
    Machado over 8 years
    @HolgerH what exactly does "style Name" represent? Is it the StyleName propertie from the ListBox? Or is it LBItem.FindStyleResource('icon') the solution?