Board index » delphi » Change the font of one item in TListBox?

Change the font of one item in TListBox?

How do I change the font of just one item in a TListBox  that contains
more than one item.  I want the other items to display normally?  I
understand that I will have to use owner-draw but I don't know how to do
that.  A code sample would be greatly appreciated.

Thanks!

Jim

 

Re:Change the font of one item in TListBox?


View a demo in Delphi\Demos\Ownerlst directory.

--
With best regards, Mike Shkolnik.
FIDO: 2:463/106.14
E-Mail: mshkol...@rs-ukraine.kiev.ua
        m...@woccu.freenet.kiev.ua
WEB: http://www.geocities.com/SiliconValley/Grid/3989

Jim Elden ??? a ???? <3768ECE4.C7392...@nojunk.accumedic.com> ...

Quote
>How do I change the font of just one item in a TListBox  that contains
>more than one item.  I want the other items to display normally?  I
>understand that I will have to use owner-draw but I don't know how to do
>that.  A code sample would be greatly appreciated.

>Thanks!

>Jim

Re:Change the font of one item in TListBox?


Hi Jim!

Quote
> How do I change the font of just one item in a TListBox  that contains
> more than one item.  I want the other items to display normally?  I
> understand that I will have to use owner-draw but I don't know how to do
> that.  A code sample would be greatly appreciated.

Set ListBox style to lbOwnerDrawFixed and on OnDrawItem event write some
like this:

(The Item #5 is the item you wish to show with different font.)

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
 with ListBox1.Canvas do begin
   FillRect(Rect);
   // If Index is the special item change font properties
   if Index = 5 then begin
      Font.Name := 'Verdana';
      Font.Size := 8;
      Font.Color := clRed;
   end else
      // Else set the default font
      Font := ListBox1.Font;

   TextOut( Rect.Left + 2, Rect.Top, ListBox1.Items[Index] );
 end;
end;

Maybe you need to set ListBox style to lbOwnerDrawVariable because not
all fonts with same size have the same Height.

Francesc Puigdemont

Other Threads