2012-11-03 84 views
10

我正嘗試解決在C#Windows Forms應用程序中的ComboBox下拉列表中突出顯示突出顯示的顏色的問題。 我已經搜索了整個網絡的答案,我發現迄今爲止最好的選擇是當正在繪製選定的項目時畫一個所需顏色的矩形。更改突出顯示組合框的顏色

Class Search 
{ 
    Public Search() 
    { 
    } 

    private void addFilter() 
    { 
     ComboBox field = new ComboBox(); 

     field.Items.AddRange(new string[] { "Item1", "item2" }); 
     field.Text = "Item1"; 
     field.DropDownStyle = ComboBoxStyle.DropDownList; 
     field.FlatStyle = FlatStyle.Flat; 
     field.BackColor = Color.FromArgb(235, 235, 235); 
     field.DrawMode = DrawMode.OwnerDrawFixed; 
     field.DrawItem += field_DrawItem; 
    } 

    private void field_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     if (e.Index >= 0) 
     { 
      ComboBox combo = sender as ComboBox; 

      if (e.Index == combo.SelectedIndex) 
       e.Graphics.FillRectangle(new SolidBrush(Color.Gray), 
             e.Bounds 
             ); 
      else 
       e.Graphics.FillRectangle(new SolidBrush(combo.BackColor), 
             e.Bounds 
             ); 

      e.Graphics.DrawString(combo.Items[e.Index].ToString(), e.Font, 
            new SolidBrush(combo.ForeColor), 
            new Point(e.Bounds.X, e.Bounds.Y) 
           ); 
     } 
    } 
} 

這段代碼的問題是,一旦在下拉菜單中的另一項選擇,其他項目我繪製一個矩形仍與我想強調的顏色。 然後我試圖挽救最後繪製的項目,並重繪:

Class Search 
{ 
    private DrawItemEventArgs lastDrawn; 

    Public Search() 
    { 
     lastDrawn = null; 
    } 

    private void addFilter() 
    { 
     ComboBox field = new ComboBox(); 

     field.Items.AddRange(new string[] { "Item1", "item2" }); 
     field.Text = "Item1"; 
     field.DropDownStyle = ComboBoxStyle.DropDownList; 
     field.FlatStyle = FlatStyle.Flat; 
     field.BackColor = Color.FromArgb(235, 235, 235); 
     field.DrawMode = DrawMode.OwnerDrawFixed; 
     field.DrawItem += field_DrawItem; 
    } 

    private void field_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     if (e.Index >= 0) 
     { 
      ComboBox combo = sender as ComboBox; 
      if (e.Index == combo.SelectedIndex) 
      { 
       e.Graphics.FillRectangle(new SolidBrush(Color.Gray), e.Bounds); 
       if(lastDrawn != null) 
        lastDrawn.Graphics.FillRectangle(new SolidBrush(combo.BackColor), 
               lastDrawn.Bounds 
               ); 
       lastDrawn = e; 
      } 
      else 
       e.Graphics.FillRectangle(new SolidBrush(combo.BackColor), 
             e.Bounds 
             ); 

      e.Graphics.DrawString(combo.Items[e.Index].ToString(), e.Font, 
            new SolidBrush(combo.ForeColor), 
            new Point(e.Bounds.X, e.Bounds.Y) 
           ); 
     } 
    } 
} 

這行返回,因爲lastDrawn.Bounds的錯誤(不兼容的類型)

lastDrawn.Graphics.FillRectangle(new SolidBrush(combo.BackColor), 
               lastDrawn.Bounds 
               ); 

我感覺,改變高亮顏色的下拉是不可能的。 在此先感謝!

回答

20

如果您在項目中的多個地方使用ComboBox,那麼一遍又一遍地重複執行DrawItem事件的相同代碼是沒有意義的。就在這個類添加到您的項目,你將有一個具有HightlightColor財產,這將使得它更容易使用控制遍佈項目中的新ComboBox控件:

class AdvancedComboBox : ComboBox 
{ 
    new public System.Windows.Forms.DrawMode DrawMode { get; set; } 
    public Color HighlightColor { get; set; } 

    public AdvancedComboBox() 
    { 
     base.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; 
     this.HighlightColor = Color.Gray; 
     this.DrawItem += new DrawItemEventHandler(AdvancedComboBox_DrawItem); 
    } 

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

     ComboBox combo = sender as ComboBox; 
     if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) 
      e.Graphics.FillRectangle(new SolidBrush(HighlightColor), 
            e.Bounds); 
     else 
      e.Graphics.FillRectangle(new SolidBrush(combo.BackColor), 
            e.Bounds); 

     e.Graphics.DrawString(combo.Items[e.Index].ToString(), e.Font, 
           new SolidBrush(combo.ForeColor), 
           new Point(e.Bounds.X, e.Bounds.Y)); 

     e.DrawFocusRectangle(); 
    } 
} 
相關問題