1

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

Loststar
  • 57
  • 9

2 Answers2

2

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

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54
  • Thanks for your answer, I have worked on the code you sent me, but I still have problem, I added to my post how I convert your Delphi to c++ code and how I implemented to my application, but the problem still there. I really apreciate your help. – Loststar Jan 20 '23 at 22:15
  • 1
    But I told you to **remove** items from `StyledSettings` in order to change corresponding `TextSettings`. And further, "use the `>>` operator to remove members from a set". In your code you are using `<<`. – Tom Brunberg Jan 21 '23 at 06:37
0

After your help my code in a Multi-Device Application C++ Builder project result on the next code:

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

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

        // 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 = "Consolas";
        lbItem->TextSettings->Font->Size = 18;

        delete lbItem;
    }

If you are trying to do it in a Windows VCL Aplication C++ Builder Project is easier (You can change the font of the entire TListBox once):

    ingredientsDataBase->Font->Size = 8.5;
    ingredientsDataBase->Font->Name = "Consolas";
Loststar
  • 57
  • 9