2012-10-30 247 views
1

可能重复:
Background color of a ListBox item (winforms)更改列表框的背景颜色

我有一个列表框,对于每一个坐标,我需要有不同的颜色。恩。

23,34 //red background 
56,78 //green background 
90,2 // yellow background 

代码:

 for (int i = 0; i < il_kl; i++) 
     { 
      int il_pkt = Klastry[i].Punkty.Count; 
      string color = lista_kolor[i]; 
      Brush mybrush = (Brush)new BrushConverter().ConvertFromString(color); 

      for (int j = 0; j < il_pkt; j++) 
      { 
       x = Klastry[i].Punkty[j].X; 
       y = Klastry[i].Punkty[j].Y; 

       _mn.kolekcje_wsp.Items.Add(x + " , " + y); 
       _mn.kolekcje_wsp.Foreground = mybrush; 

      } 

     } 

我使用Foreground了,但如何改变Background颜色为每个坐标?

回答

0

你可以尝试以下,看看它是否适合你

大概是唯一的方式来实现这一目标是绘制自己的项目。

设置DrawMode到OwnerDrawFixed

和代码像这样的DrawItem事件:

private void listBox_DrawItem(object sender, DrawItemEventArgs e) 
{ 
    e.DrawBackground(); 
    Graphics myCustomGraphic = e.Graphics; 
    myCustomGraphic.FillRectangle(new SolidBrush(Color.Yellow), e.Bounds); 
    // Print text 
    e.DrawFocusRectangle(); 
}