954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Color Different Lines of Text in Listbox

Making a logbook using editbox for entries that fill a listbox. I have two enter buttons. One would enter line in Red one in Black but I don't know how to change the font color in the listbox. My first pascal program and this is the last step for completion.:( I'm total noob so please explain clearly.

BorisSobad
Newbie Poster
2 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

You cannot simply change the font per line in a TListBox, without adding some additional coding.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 
You cannot simply change the font per line in a TListBox, without adding some additional coding.

Yes I know. Change the event of ondrawItem. But can it be done with an easy bit of code. I know how to color an individual line or every other line but not how to tie the color to anything but the index.

BorisSobad
Newbie Poster
2 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Well, if you know that already then it is relatively simple. The Items property of a TListBox has an AddObject method. You can use it when adding your string like this:

// clRed is an Integer, and you just cast it to an object
// so it can be added alongside your item text.
ListBox1.Items.AddObject('item', TObject(clRed));

// In your OnDrawItem (color is a TColor variable)
// where i is the index of your current item.
color := TColor(ListBox.Items.Objects[i]);
pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: