Board index » delphi » Listbox component and different colors ?

Listbox component and different colors ?

hi there!

i'd like to use different colors for just one line (item) in a listbox
component !!!

for example;

xxx       yyy      zzz       ------> items[0]
----       ----      -----
will       will      will
be        be       be
blue     red     yellow

thanks for your helps!

 

Re:Listbox component and different colors ?


In the OnClick event of the ListBox, try something similar to this:
(Note that this will change the color of the entire list box)

procedure TForm1.ListBox1Click(Sender: TObject);
var
  lb : TListBox;
  color : TColor;

begin
   if Sender is TListBox then
   begin
      lb := Sender as TListBox;
      case lb.ItemIndex of
          0 : { 'Item #1' } color := clBlue;
          1 : { 'Item #2' } color := clRed;
          2 : { 'Item #3' } color := clYellow;
      else
          color := clLime;
      end;
      lb.Color := color;
   end;
end;

HTH

Rick

Re:Listbox component and different colors ?


Give you listbox a Style of lbOwnerDrawFixed and put the following in
the OnDrawItem event handler:

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index:
Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  s: String;
begin

  s := ListBox1.Items[Index];
  ListBox1.Canvas.FillRect(Rect);
  if Index = 0 then
  ListBox1.Canvas.Font.Color := clRed;
  DrawText(ListBox1.Canvas.Handle, PChar(s), Length(s), Rect, dt_Left
or dt_VCenter);

end;

 --- x
==============================
Xavier Pacheco (TeamB)
xav...@xapware.com

Sorry but TeamB cannot answer support
questions received via email.

Other Threads