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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wordwrap in TListBox

Status
Not open for further replies.

MoonchildHK

Programmer
Jun 5, 2001
22
HK
Hi All,

I am writing an IRC client. For the main message list I am using a TListBox set to LBOwnerDrawFixed. I do this because I want to display the Username first (in one color) and then the message (in another color). Everything works fine except that the ListBox has no word wrap..

When I tried to write a function to do it myself.. I ran into problems. I tried using the Canvas->TextWidth function and testing the message to be shown, starting from 1 characted only and adding more until the value = the width of the box, then splitting the message at that point to create a second entry in the ListBox etc.

This doesn't work however.. because when someone re-sizes the form, I have to re-split the message up, which means there could be more or less items in the ListBox's Items list, and thing's got really complicated :-(

Does anyone have any idea for another approach to this problem? I have got myself bogged down in horrible code.. and can't seem to find an easy way out!

I though about using a TMemo which would allow multi-line, but I can not do colors on this because there is no way overwrite the onDraw method??

I would rather avoid buying a commercial TListBox replacement which has WordWrap.

Any ideas would be gratefully received.

Thanks,

Steph
 
A RichEdit has colors. see the Example->Apps->RichEdit in the BCB folder, it has good examples of how to do colors. The only problem there is that it doesn't show how to do something like the name in one color, and the rest of the text in another. You have to figure that out by yourself. If you do, tell me, because I've been stuck on that for weeks now. Cyprus
 
I cant use a RichEdit because there is no way to write my own OnDraw event for it (unless someone knows how to override the built in one??)

As for the name in one color and the message in another, I create a structure contianing the message, date and username for each message, then attach it to the Object property of te string list (TListBox uses TStringList to store it's messages, which allows an object (of any type) to be attached to each string).

Then I set the ListBox to be OwnerDrawn, which means I write the function to do the actual displaying. For each line, I change the font color first (Canvas->Font->Color) and then display the Username, change the color back and write the message.

There should be a way to over write the built in OnDraw method for a TRichEdit or TMemo, but I can't figure out how :-(

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top