C# code needed for listbox that can display text items of different lengths

My listbox needs to display text items of different lengths: one line to a few lines long. I want the longer items to wrap instead of being truncated.
What do I need to do this?

Also, if I choose not to do the multiline display and truncate the text but show the rest of the text (by extending it outside the listbox) when mouse is over the item. How do I do this?

Many thanks.
rj94070Asked:
Who is Participating?
 
Jens FiedererConnect With a Mentor Test Developer/ValidatorCommented:
If you want variable height items, you can draw them yourself.  Set the ListBox's DrawMode to OwnerDrawVariable and define the MeasureItem and DrawItem events.  MeasureItem will have to decide how large a box to allocate to the item, and DrawItem will do the actual display using the Graphics functions.

MSDN has a code example for the fixed height version at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformslistboxclassdrawitemtopic.asp

Your version would be pretty tedious - you could simply do a DrawString to a bitmap of sufficient width, and then chop up the bitmap and place strips one over the other to display rows in your listbox, or you could try to break the text at logical places such as spaces.

For the second version, it would be MUCH easier to display the full text in something like a status line using the MouseHover event (you still have to figure out over which item the mouse is hovering).  If you really DO want to draw an extended box at the location in the listbox, make sure you do a redraw when the mouse moves to another item or leaves the control!






0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.