更改ListBox背景颜色

时间:2012-10-30 20:45:33

标签: c# listbox

  

可能重复:
  Background color of a ListBox item (winforms)

我有一个ListBox,我需要为每个坐标设置不同的颜色。离。

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颜色?

1 个答案:

答案 0 :(得分: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();
}