Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

different coloured text in TMemo or TListBox 1

Status
Not open for further replies.

ecynaxt

Programmer
Mar 20, 2002
11
GB
Does any body know a way to have text in different colours in a TMemo or TListBox. I am after a way of making only some items in a list appear in a different colour to make them stand out.

 
Use a RichEdit instead. You can then select portions of text and set the attributes for the selected text only. See SelStart etc in the Delphi help for some good examples.


When your feeling down and your resistance is low, light another cigarette and let yourself go [rockband]
 
Just what I need. Thanks. Should of thought of that really.
 
Sorry, I was wrong in my original post. The control I am using is a TCheckListBox. I tried out the RichEdit control and that worked fine, but i would lose my check boxes.

Is it possible to change the text colour in a TCheckListBox, maybe by customising the control in some way?
 
This is fairly easy to do.

Set the style property of your TCheckListBox to lbOwnerDrawFixed.

Then write a OnDrawItem event handler for your TCheckListBox. Here is a sample event handler for a CheckListBox called cb. In the event handler I am going to make the background color of odd numbered items clAqua and the font clRed in color.
Code:
procedure TForm1.cbDrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
begin
  if Odd(Index) then begin
    cb.canvas.brush.color := clAqua;
    cb.Canvas.Font.color := clRed;  
  end;
  cb.canvas.TextRect( Rect, rect.left, rect.top, cb.items[index] );
end;
Of course, you will want to do something more sophisticated but the secret is to make the Style of the Control either lbOwnerDrawFixed or lbOwnderDrawVariable and write a suitable OnDrawItem event handler.

The Delphi Help is quite good at describing this.

Andrew
 
Sounds like it can be adapted to what I'm after. Thanks for that Andrew

My other idea was to have a richedit right next to a checklistbox to get the best of both worlds. Its a bit of a bodge but fell down because they would need to scroll in sync.

Its a bit off topic but do you know of a way to make two controls scroll in sync by just moving the scrollbar on one?

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top