Custom drawing of TCustomListbox items


I’m rewriting a VCL component showing a customized TCustomListbox to Firemonkey in Delphi 10.2. The customization used an overridden DrawItem, basically adding some indentation and setting the text color depending on the item text and index.

DrawItem made it rather easy, but there seem to be nothing like that in FMX. I can override PaintChildren and draw every item myself, but then it looks differently and I have to deal with scrolling and everything myself. I’m just starting with FMX and don’t have the sources yet.

Is there a DrawItem replacement in FMX? I may have missed it.
If not, how do it get the needed information? Basically, the rectangle to draw in and ideally the style used.


The solution by Hans works, but has some major problems:


Setting the color doesn’t work, the text is always black. I tried various possibilities including this one:

TextSettings.FontColor := TAlphaColorRec.Red;


Opening a box with 180 Items takes maybe two seconds. We need that many items and their count is actually the reason why we need a customized box (we provide filtering using the TEdit part of our component). A version using strings without TMyItem was faster (though probably slower than the VCL version), but using these items seems to slow it down even more (it’s slower than filling an HTML list styled similarly).

Or something else? Having no sources and practically no documentation I can’t tell.

I tried to cache the items for reuse, but this didn’t help.

It looks like using custom items is actually faster than using strings, (timing in milliseconds):

nItems String TMyItem
200 672 12
2000 5604 267
20000 97322 18700

The speed problem seems to accumulate when the content changes multiple times. I was using FListBox.Items.Clear;, then I tried

n := FListBox.Items.Count;
FOR i := 0 TO n-1 DO FListBox.ListItems[n-1-i].Free;

and finally FListBox.Clear;, which makes most sense (and which I found last). Still, in the end it seems to need 2 ms per item.

Comments are closed.