ba272
asked on
I'm losing the bottom of the character when displaying in a multi-color, selected item bold, listbox
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.
Thanks,
Bob
private void ordersListBox_DrawItem(obj ect sndr, System.Windows.Forms.DrawI temEventAr gs e)
{
//
// Draw the background of the ListBox control for each item.
// Create a new Brush and initialize to a Black colored brush by default.
//
e.DrawBackground();
// 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.SelectedInde x )
font = boldFont;
e.Graphics.FillRectangle(b ackBrush, e.Bounds);
e.Graphics.DrawString(((Li stBox)send er).Items[ e.Index].T oString(), font,
fontBrush,e.Bounds,StringF ormat.Gene ricDefault );
//
// If the ListBox has focus, draw a focus rectangle around the selected item.
//
e.DrawFocusRectangle();
}
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.
Thanks,
Bob
private void ordersListBox_DrawItem(obj
{
//
// Draw the background of the ListBox control for each item.
// Create a new Brush and initialize to a Black colored brush by default.
//
e.DrawBackground();
// 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.SelectedInde
font = boldFont;
e.Graphics.FillRectangle(b
e.Graphics.DrawString(((Li
fontBrush,e.Bounds,StringF
//
// If the ListBox has focus, draw a focus rectangle around the selected item.
//
e.DrawFocusRectangle();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
DrawMode property of listbox should be OwnerDrawVariable.
ASKER
Thanks.