1
votes

I'm trying to change a ListBoxItem's font color at runtime. I'm able to change the color with no issue at design time by using the FontColor property, but I need to change the font color at runtime. The example below is not changing the font color.

var
    ListBoxItem : TListboxItem;
begin
    ListBoxItem := TlistBoxItem.Create(ListBox1);

    ListBoxItem.Text := Edit1.Text;
    ListBoxItem.ItemData.Detail := Edit2.Text;
    ListBoxItem.StyleLookup := 'listboxitembottomdetail';
    ListBoxItem.TextSettings.FontColor := TAlphaColorRec.Blue;
    ListBoxItem.Height := 34;
    ListBox1.AddObject(ListBoxitem);

end;

The code above is supposed to add an item to ListBox1 with the defined color, but it just adds the item with the default font color.

1
Add ListBoxItem.StyledSettings := ListBoxItem.StyledSettings - [TStyledSetting.FontColor];.Victoria

1 Answers

2
votes

The following is tested for Delphi 10.1 Berlin

In order to change both the normal and detail font color you can make use of the StyleLookup property with your own custom style.

At design time in your main form create a TListBox and add a TListBoxItem. Go to the TListBoxItem's properties and set the StyleLookup to listboxitembottomdetail. After this right click the TListBoxItem and choose Edit custom style...

Now in the style designer open up the tree ListBoxItem1Style1. Here you will find the text and detail items. There can adjust the font color to whatever you want. Make sure to save.

Then after adding the style you can adjust your code to something like this:

var
    ListBoxItem : TListboxItem;
begin
    ListBoxItem := TListBoxItem.Create(ListBox1);
    ListBoxItem.Text := Edit1.Text;
    ListBoxItem.ItemData.Detail := Edit2.Text;
    ListBoxItem.StyleLookup := 'ListBoxItem1Style1';
    ListBoxItem.Height := 34;
    ListBox1.AddObject(ListBoxitem);
end;