firemonkeyvclrad-studio

How can I change the Font of a TListBox's items?


I'm building an app with RAD Studio 11, but I can't find a way to change the item font of my TListBox.

App

I tried to change TListBox.Font in the Object Inspector, but when I select my TListBox called ingredientsDataBase in the Object Inspector, I can just change TListBox settings instead of TListBox.Items settings.

Object Inspector

I add a TListBoxItem manually as follow:

ListBoxItem

Then, I can change ListBoxItem1.Font in the Object Inspector, after selecting my ListBoxItem1 (no problem).

ListBoxItem1 Font changed

The problem is that, when I run my program, the Font change only affects my ListBoxItem1, and I want the same Font for every item that I add to my TListBox.

image

UPDATE 1

After your help I tried to convert your Delphi code to C++.

__fastcall TIngredientCreator::addButtonClick(TObject *Sender){
        //More code Here
        //Then I ADD a new ListBoxItem to my ListBox "ingredientsDataBase"
        ingredientsDataBase->Items->Add("newIngredient");

        TListBoxItem *lbItem = new TListBoxItem(ingredientsDataBase);
        lbItem->Parent = ingredientsDataBase;

        // Remove Family and Size from the items TStyledSettings
        lbItem->StyledSettings = lbItem->StyledSettings << TStyledSetting::Family << TStyledSetting::Size;

        // You can now set these TextSettings as needed
        lbItem->TextSettings->Font->Family = "Algerian";
        lbItem->TextSettings->Font->Size = 18;
        lbItem->Text = "algerian";

        delete lbItem;
    }

There is no syntax error, but I can't associate my new ListBoxItem, in this case the Text or Name of that new ListBoxItem called "newIngredient" (I don't know how to do it in this code), so when I run my program nothing happen to mi new Item at least.

UPDATE 2

I found a way to associate my newIngredient to TListBoxItem Object as follow:

 int index = ingredientsDataBase->Items->IndexOf(newIngredient);
lbItem = ingredientsDataBase->ItemByIndex(index);

When I run the code and I add a new Ingredient, just the Text of the newIngredient is changed to "algerian", because in the first code I have this line lbItem->Text = "algerian" all good here. But Font and Size still without change.

Thanks for your answers


Solution

  • When you add items to the listbox, you need to clear some items from the default StyledSettings property of the new item, if you want to modify the corresponding TextSettings.

    Here's an example in Delphi to do what you want:

    procedure TForm5.Button2Click(Sender: TObject);
    var
      lbItem: TListBoxItem;
    begin
      lbItem := TListBoxItem.Create(ListBox1);
      lbItem.Parent := ListBox1;
      // Remove Family and Size from the items TStyledSettings
      lbItem.StyledSettings := lbItem.StyledSettings - [TStyledSetting.Family,TStyledSetting.Size];
      // You can now set these TextSettings as needed
      lbItem.TextSettings.Font.Family := 'Algerian';
      lbItem.TextSettings.Font.Size := 18;
      lbItem.Text := 'algerian';
    
      // In Embarcadero C++Builder you use the ">>" operator to remove members from a set, and "<<" to include them.
    end;
    

    enter image description here