Hide empty space behind invisible list box items


I can see other people have had this issue, and the proposed solution is not working for me.

I have a list box with a number of static items populated in design-time, and in run-time, some list box items have their visibility changed. However, those items which are not visible leave an empty space in the list box where that item belongs. It’s proposed to set the invisible item height to 0, and it’s apparently worked for people, but it’s not working for me.

This is a common function I wrote to accommodate for this:

procedure FixHidden(AListBox: TListBox);
X: Integer;
I: TListBoxItem;
for X := 0 to AListBox.Count-1 do begin
I:= AListBox.ItemByIndex(X);
if I.Visible then
I.Height:= AListBox.ItemHeight
I.Height:= 0;

While stepping through the code, I can confirm that after I.Height:= 0; the Height is in fact 0. However, the problem still persists, and there’s still an empty void in the list box, as if the list box control ignores what height I set the item.

How can I make invisible items truly hidden from the list box including this empty space behind it, without deleting these items? My app has many different list box controls with the same issue, so a global solution would be ideal.

NOTE: This is using Windows 32 as a platform.


I did a few more trials, and discovered something. In design-time, the list box items Height property defaults to the parent list box’s ItemHeight property. If I change the Height of a specific item in design-time, it immediately reverts back to what it was before. So the question then becomes… why is Firemonkey reverting TListBoxItem.Height every time I change it?

Comments are closed.