C++Builder logo
TListBox :
Changing the font color/style of an item
TListBox:
Changer la couleur ou le style d'un item

Changing the font color or the font style of an item in a ListBox is pretty straightforward since the TListBox style property  can be set to OwnerDraw. The TListBox component provides us an event OnDrawItem wich is called whenever an item needs to be redrawn. Modifier la couleur ou le style d'un élément dans une ListBox est relativement aisé grâce à la propriété style du TListBox qui peut être mise à OwnerDraw. Le composant nous signale qu'un élément de la ListBox doit être redessiné au moyen d'un événement OnDrawItem.
1. Changing the color Modification de la couleur
The code below shows you how to change the color of the fourth item (index==3) of a ListBox. Don't forget to set the style property of the ListBox to OwnerDrawFixed and populate the ListBox with some elements. Next put the code in the OnDrawItem event. Le code ci-dessous montre comment changer la couleur du quatrième élément de la ListBox (index==3). Auparavent, n'oubliez pas de rendre votre ListBox OwnerDraw en spécifiant le style OwnerDrawFixed. Ajoutez ensuite quelques éléments à la ListBox et copiez le code dans l'événement OnDrawItem.

void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int Index, 
      TRect &Rect, TOwnerDrawState State) 

  ListBox1->Canvas->FillRect(Rect); 
  if(Index==3) 
  { 
    ListBox1->Canvas->Font->Color=clRed; 
  } 
  else 
  { 
    ListBox1->Canvas->Font->Color=clBlack; 
  }
  ListBox1->Canvas->TextOut(Rect.Left+2,Rect.Top,ListBox1->Items->Strings[Index]); 
}


If you want to change the color of the selected item (when you click on it) change the code as shown here. Si vous voulez changer la couleur de l'élément sélectionné lorsque vous avez cliqué dessus, modifiez le code comme indiqué ci-dessous.

void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int Index, 
      TRect &Rect, TOwnerDrawState State) 

  ListBox1->Canvas->FillRect(Rect); 
  if(State.Contains(odFocused)) 
  { 
    ListBox1->Canvas->Font->Color=clRed; 
  } 
  else 
  { 
    ListBox1->Canvas->Font->Color=clBlack; 
  } 
  ListBox1->Canvas->TextOut(Rect.Left+2,Rect.Top,ListBox1->Items->Strings[Index]); 
}


2. Changing the style of an item Modification du style d'un élément
Ok, I can change the color so I can change the font style too. All I have to do is modify the line :
ListBox1->Canvas->Font->Color=clRed;
with : 
ListBox1->Canvas->Font->Style = ListBox1->Canvas->Font->Style<<fsBold;
for instance.
If you change the line with this one :
ListBox1->Canvas->Font->Style = TFontStyles()<<fsBold;
It's alright but TFontStyles()<<fsBold  resets the style and doesn't add a new style to an existing one. 
I prefer to use a temporary TFont object  :
Bon, si je peux changer la couleur, je peux changer le style, par exemple afficher l'élément en gras. Tout ce que j"ai à faire est de remplacer la ligne :
ListBox1->Canvas->Font->Color=clRed;
par celle-ci :
ListBox1->Canvas->Font->Style = ListBox1->Canvas->Font->Style<<fsBold;
par exemple.
Si vous modifier la ligne comme ceci :
ListBox1->Canvas->Font->Style = TFontStyles()<<fsBold;
c'est ok mais TFontStyles()<<fsBold resette tous les styles qui auraient été définis avant. Pour éviter cela, je préfere utiliser temporairement un object TFont auquel vous pouvez assigner toutes les valeurs que vous voulez:

void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int Index, 
      TRect &Rect, TOwnerDrawState State) 

  TFont *LBFont=new TFont; 
  LBFont->Name=ListBox1->Font->Name; 
  LBFont->Size=ListBox1->Font->Size; 
  LBFont->Style=LBFont->Style<<fsBold; 
  LBFont->Color=clRed; 
  ListBox1->Canvas->FillRect(Rect); 
  if(State.Contains(odFocused)) 
  { 
    ListBox1->Canvas->Font->Assign(LBFont); 
  } 
  else 
  { 
    ListBox1->Canvas->Font->Assign(ListBox1->Font); 
  } 
  ListBox1->Canvas->TextOut(Rect.Left+2,Rect.Top,ListBox1->Items->Strings[Index]); 
  delete LBFont; 
}