Kolor tła elementu ListBox (winforms)

Jak ustawić kolor tła określonego elementu w systemie.Okna.Formularze.ListBox? Chciałbym móc ustawić wiele z nich, jeśli to możliwe.

Author: Dested, 2008-09-18

5 answers

Prawdopodobnie jedynym sposobem, aby to osiągnąć, jest samodzielne narysowanie przedmiotów.

Ustaw DrawMode na OwnerDrawFixed

I Zakoduj coś takiego na zdarzeniu 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();
}

Drugą opcją byłoby użycie ListView, chociaż mają one inny sposób implementacji (nie tak naprawdę związany z danymi, ale bardziej elastyczny w postaci kolumn)

 51
Author: Grad van Horck,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-09-18 11:28:45

Dzięki za odpowiedź Grada van Horcka , poprowadziła mnie we właściwym kierunku.

Aby wspierać tekst (nie tylko kolor tła) oto mój w pełni działający kod:

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

Powyższe dodaje do podanego kodu i wyświetli właściwy tekst oraz podświetli wybrany element.

 53
Author: Shadow Wizard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:17:55
// 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;

Jeśli przez ustawienie wielu kolorów tła rozumiesz ustawienie innego koloru tła dla każdego elementu, nie jest to możliwe w przypadku ListBox, ale w przypadku ListView, z czymś takim jak:

// Set the background of the first item in the list
MyListView.Items[0].BackColor = Color.Red;
 2
Author: Matthew Scharley,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-09-18 11:31:08
     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();
    }

Pełna Próbka

 0
Author: Thulani Chivandikwa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-09 06:55:56
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);
        }
 0
Author: Serdar Karaca,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-13 09:44:38