I'm losing the bottom of the character when displaying in a multi-color, selected item bold, listbox

Posted on 2004-11-20
Last Modified: 2013-12-03
Hi,  I'm having a problem with the following code.  It successfully lets me display individual items in a listbox in any color I choose, and it will highlight the selected item by writing it as bold.  The problem is that it is chopping off the bottom of the characters.  Characters like 'g' and y' get screwed up, presumably because they get overwritten when I call FillRectangle for the item which follows.

Does anyone have any ideas?  Obviously it involves the fact that I'm filling a rectangle with
Brushes.Green before I write the string.  But I can't think of any way around this.


private void ordersListBox_DrawItem(object sndr, System.Windows.Forms.DrawItemEventArgs e)
    // Draw the background of the ListBox control for each item.
    // Create a new Brush and initialize to a Black colored brush by default.
// here you can set the colors to anything you want
    Brush fontBrush = Brushes.Black;
    Brush backBrush = Brushes.Green;
    Font boldFont = new Font(e.Font, FontStyle.Bold);

    Font font = e.Font;
    if ( e.Index == ordersListBox.SelectedIndex )
        font = boldFont;
    e.Graphics.FillRectangle(backBrush, e.Bounds);
    e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(), font,      
    // If the ListBox has focus, draw a focus rectangle around the selected item.
Question by:ba272
    LVL 48

    Accepted Solution

    For owner-drawn control you need to handle also MeasureItem. In this event handler supply item height which is enough to show all string.

    Author Comment

    I see how to to create a MeasureItem event, but I can't seem to get it to reach my breakpoint inside it.  Is there someting I need to do in order to make this event fire?

    LVL 48

    Expert Comment

    DrawMode property of listbox should be OwnerDrawVariable.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    This video discusses moving either the default database or any database to a new volume.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    760 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

    15 Experts available now in Live!

    Get 1:1 Help Now