Listbox manual DrawItem big font size

你。 提交于 2019-12-05 18:15:34

问题


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:

But with default font size, everything is correct.

What is the problem?


回答1:


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;
}


来源:https://stackoverflow.com/questions/8835568/listbox-manual-drawitem-big-font-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!