2012-04-18 104 views
0

我在組合框中創建了一個字體列表。我設置它的DrawMode到OwnerDrawFixed和方法DRAWITEM很簡單:c#combobox DrawItem - 刷新問題

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

在gerneral,它工作正常。問題出現在鼠標滾動上。然後有些項目看起來像隨機圖形,直到他們專注。任何人都知道問題的解決方案?

回答

1

總是無論索引如何,都調用e.DrawBackground()。修復:

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

感謝您發佈,但它沒有幫助:\ \ – Ray 2012-04-19 08:40:09