2008-09-18 493 views

回答

50

可能唯一的方法就是自己畫物品。

設置DrawModeOwnerDrawFixed

和代碼像這樣的DrawItem事件:

private void listBox_DrawItem(object sender, DrawItemEventArgs e) 
{ 
    e.DrawBackground(); 
    Graphics g = e.Graphics; 

    g.FillRectangle(new SolidBrush(Color.Silver), e.Bounds); 

    // Print text 

    e.DrawFocusRectangle(); 
} 

第二個選項是使用一個ListView,雖然他們有實現的其他方式(不是真的數據束縛,但在列的方式)

2
// Set the background to a predefined colour 
MyListBox.BackColor = Color.Red; 
// OR: Set parts of a color. 
MyListBox.BackColor.R = 255; 
MyListBox.BackColor.G = 0; 
MyListBox.BackColor.B = 0; 

如果你的意思是通過設置多重背景和什麼更靈活d顏色是設置爲每個項目不同的背景顏色,這是不可能與一個ListBox,但有一個ListView IS,喜歡的東西:

// Set the background of the first item in the list 
MyListView.Items[0].BackColor = Color.Red; 
+2

有可能用一個ListBox。請參閱http://stackoverflow.com/questions/91747/background-color-of-a-listbox-item-winforms#91758 – jfs 2008-09-18 11:35:44

+0

s/possible/easy /。好吧。 C#1,新手0.我以前在重載繪畫方法方面工作不多。 – 2008-09-18 11:38:30

52

感謝answer by Grad van Horck,它指導我在正確的方向。

爲了支持文本(不只是背景色)這裏是我完全工作代碼:

//global brushes with ordinary/selected colors 
private SolidBrush reportsForegroundBrushSelected = new SolidBrush(Color.White); 
private SolidBrush reportsForegroundBrush = new SolidBrush(Color.Black); 
private SolidBrush reportsBackgroundBrushSelected = new SolidBrush(Color.FromKnownColor(KnownColor.Highlight)); 
private SolidBrush reportsBackgroundBrush1 = new SolidBrush(Color.White); 
private SolidBrush reportsBackgroundBrush2 = new SolidBrush(Color.Gray); 

//custom method to draw the items, don't forget to set DrawMode of the ListBox to OwnerDrawFixed 
private void lbReports_DrawItem(object sender, DrawItemEventArgs e) 
{ 
    e.DrawBackground(); 
    bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected); 

    int index = e.Index; 
    if (index >= 0 && index < lbReports.Items.Count) 
    { 
     string text = lbReports.Items[index].ToString(); 
     Graphics g = e.Graphics; 

     //background: 
     SolidBrush backgroundBrush; 
     if (selected) 
      backgroundBrush = reportsBackgroundBrushSelected; 
     else if ((index % 2) == 0) 
      backgroundBrush = reportsBackgroundBrush1; 
     else 
      backgroundBrush = reportsBackgroundBrush2; 
     g.FillRectangle(backgroundBrush, e.Bounds); 

     //text: 
     SolidBrush foregroundBrush = (selected) ? reportsForegroundBrushSelected : reportsForegroundBrush; 
     g.DrawString(text, e.Font, foregroundBrush, lbReports.GetItemRectangle(index).Location); 
    } 

    e.DrawFocusRectangle(); 
} 

以上添加到給定的代碼,並會顯示正確的文字加上高亮所選項目。

0
 public Picker() 
    { 
     InitializeComponent(); 
     this.listBox.DrawMode = DrawMode.OwnerDrawVariable; 
     this.listBox.MeasureItem += listBoxMetals_MeasureItem; 
     this.listBox.DrawItem += listBoxMetals_DrawItem; 
    } 

    void listBoxMetals_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     e.DrawBackground(); 
     Brush myBrush = Brushes.Black; 
     var item = listBox.Items[e.Index] as Mapping; 
     if (e.Index % 2 == 0) 
     { 
      e.Graphics.FillRectangle(new SolidBrush(Color.GhostWhite), e.Bounds); 
     } 
     e.Graphics.DrawString(item.Name, 
      e.Font, myBrush, e.Bounds, StringFormat.GenericDefault); 
     e.DrawFocusRectangle(); 
    } 

完整的示例

0
private void listbox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) 
     { 
      e.DrawBackground(); 
      Brush myBrush = Brushes.Black; 
       var item = listbox1.Items[e.Index]; 
       if(e.Index % 2 == 0) 
       { 
        e.Graphics.FillRectangle(new SolidBrush(Color.Gold), e.Bounds); 
       } 


      e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(), 
       e.Font, myBrush,e.Bounds,StringFormat.GenericDefault); 
      e.DrawFocusRectangle(); 
     } 


public MainForm() 
     { 
      InitializeComponent(); 
      this.listbox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listbox1_DrawItem); 
     }