【问题标题】:Change a ListBox Background Color [duplicate]更改列表框背景颜色 [重复]
【发布时间】:2012-10-20 06:49:10
【问题描述】:

可能重复:
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 颜色?

【问题讨论】:

    标签: c# listbox


    【解决方案1】:

    您可以尝试以下方法,看看它是否适合您

    可能实现这一点的唯一方法是自己绘制项目。

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

    【讨论】:

      猜你喜欢
      • 2012-10-23
      • 2015-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多