Domanda

Come posso impostare il colore di sfondo di un elemento specifico in un Sistema.Windows.Le forme.ListBox?Vorrei essere in grado di impostare più, se possibile.

È stato utile?

Soluzione

Probabilmente l'unico modo per realizzare che è quello di disegnare gli elementi stessi.

Impostare il DrawMode per OwnerDrawFixed

e il codice di qualcosa di simile l'evento 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();
}

La seconda opzione sarebbe quella di usare una ListView, anche se hanno un altro modo di implementazioni (non proprio associato ai dati, ma più flessibile in modo di colonne)

Altri suggerimenti

Grazie per l' risposta da Grad van Horck, che mi ha guidato nella giusta direzione.

A sostegno di testo (non solo il colore di sfondo, qui è il mio lavoro completamente il codice:

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

Di cui sopra si aggiunge al dato codice e di mostrare il testo giusto plus evidenziare l'elemento selezionato.

// 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;

Se ciò che si intende per impostazione più colori di sfondo è di impostare un colore di sfondo diverso per ogni elemento, questo non è possibile con una ListBox, ma con un controllo ListView, con qualcosa come:

// Set the background of the first item in the list
MyListView.Items[0].BackColor = Color.Red;
public MainForm()
{
    InitializeComponent();
    this.listbox1.DrawItem += new DrawItemEventHandler(this.listbox1_DrawItem);
}

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

Esempio completo

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top