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

Posted on 2005-04-29
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.
Question by:rj94070
    1 Comment
    LVL 23

    Accepted Solution

    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

    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!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now