c#组合框DrawItem-刷新问题
我在组合框中创建了一个字体列表。我将其DrawMode设置为OwnerDrawFixed,DrawItem的方法很简单:c#组合框DrawItem-刷新问题,c#,combobox,C#,Combobox,我在组合框中创建了一个字体列表。我将其DrawMode设置为OwnerDrawFixed,DrawItem的方法很简单: void cmbFonts_DrawItem(object sender, DrawItemEventArgs e) { if (e.Index < 0) return; e.DrawBackground(); Font newFont = new Font(cmbFonts.Items[e.Index].ToString(),
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();
}
void cmbFonts\u DrawItem(对象发送方,DrawItemEventArgs e)
{
如果(e.指数<0)返回;
e、 牵引杆接地();
字体新字体=
新字体(cmbFonts.Items[e.Index].ToString(),this.DefaultFontSize);
e、 Graphics.DrawString(cmbFonts.Items[e.Index].ToString(),
新字体,
新SolidBrush(颜色为黑色),
新矩形(e.Bounds.Location,e.Bounds.Size));
e、 DrawFocusRectangle();
}
一般来说,它工作正常。鼠标滚动时出现问题。然后一些项目看起来像随机图形,直到它们被聚焦。有人知道该问题的解决方案吗?无论索引如何,始终调用e.DruckGround()。修正:
void cmbFonts_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
if (e.Index >= 0) {
// etc...
}
}