Listbox手册DrawItem大字体大小

时间:2012-01-12 13:07:25

标签: c# winforms listbox

我正在尝试绘制其末尾为红色的*字符的项目(并移除该*个字符)并绘制其他黑色项目。

这是我的代码:

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

列表框的DrawMode属性也设置为OwnerDrawVariable

当listbox的字体是默认字体时,我的代码工作正常。

但是当我将字体大小从8.25(默认大小)更改为14时,文本的一半会在列表框中绘制。像这样: My listbox when size is 14!

但是使用默认字体大小,一切都是正确的。

有什么问题?

1 个答案:

答案 0 :(得分:8)

您必须处理MeasureItem事件并设置项目的高度:

private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
     e.ItemHeight = listBox1.Font.Height;
}
相关问题