Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listbox manual DrawItem big font size

I'm trying to draw items that end of them is an * character in red (and remove that * character) and draw other items in black color.

this is my code:

    private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground() ; //Draw our regular background
        if (Microsoft.VisualBasic.Strings.Right(listBox1.Items[e.Index].ToString(), 1) == "*")
        {
            e.Graphics.DrawString(Microsoft.VisualBasic.Strings.Mid(listBox1.Items[e.Index].ToString(),1,listBox1.Items[e.Index].ToString().Length - 1), e.Font, Brushes.Red, e.Bounds);    //Draw the item text in red!
        }
        else
        {
            e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, Brushes.Black, e.Bounds); //Draw the item text in its regular color
        }
    }

Also DrawMode property of the listbox is set to OwnerDrawVariable.

My code is working fine when the font of listbox is the default font.

But When I change the font size from 8.25 (default size) to 14, half of the text draws on the listbox. like this: My listbox when size is 14!

But with default font size, everything is correct.

What is the problem?

like image 719
Mahdi Ghiasi Avatar asked Jan 12 '12 13:01

Mahdi Ghiasi


1 Answers

You have to handle the MeasureItem event and set the height of the items there:

private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
     e.ItemHeight = listBox1.Font.Height;
}
like image 196
Frédéric Hamidi Avatar answered Oct 15 '22 07:10

Frédéric Hamidi