?
Solved

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

Posted on 2005-04-29
1
Medium Priority
?
456 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:rj94070
1 Comment
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 1500 total points
ID: 13894361
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month16 days, 11 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question