0
votes

I created a font list in a combobox. I set it's DrawMode to OwnerDrawFixed and the method DrawItem is simple:

void cmbFonts_DrawItem(object sender, DrawItemEventArgs e)
{
    if (e.Index < 0) return;
    e.DrawBackground();

    Font newFont =
        new Font(cmbFonts.Items[e.Index].ToString(), this.DefaultFontSize);
    e.Graphics.DrawString(cmbFonts.Items[e.Index].ToString(),
                          newFont,
                          new SolidBrush(Color.Black),
                          new Rectangle(e.Bounds.Location, e.Bounds.Size));
    e.DrawFocusRectangle();
}

In gerneral, it works correctly. The problem appears on mouse scrolling. Then some items look like random graphics until they are focused. Anybody knows solution for the problem?

1

1 Answers

1
votes

Always call e.DrawBackground(), regardless of the index. Fix:

void cmbFonts_DrawItem(object sender, DrawItemEventArgs e)
{
    e.DrawBackground();
    if (e.Index >= 0) {
       // etc...
    }
}