سؤال

كيف يمكنني ضبط لون الخلفية لعنصر معين في System.Windows.Forms.ListBox؟أود أن أكون قادرًا على تعيين عدة منها إذا أمكن.

هل كانت مفيدة؟

المحلول

ربما تكون الطريقة الوحيدة لتحقيق ذلك هي رسم العناصر بنفسك.

تعيين DrawMode ل OwnerDrawFixed

وقم بكتابة شيء مثل هذا في حدث 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();
}

الخيار الثاني هو استخدام ListView، على الرغم من أن لديهم طريقة أخرى للتطبيق (ليست مرتبطة بالبيانات حقًا، ولكنها أكثر مرونة في طريقة الأعمدة)

نصائح أخرى

شكرا ل الإجابة بواسطة جراد فان هورك, ، لقد قادني في الاتجاه الصحيح.

لدعم النص (وليس فقط لون الخلفية) إليك رمز العمل الكامل الخاص بي:

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

يضيف ما ورد أعلاه إلى الكود المحدد وسيظهر النص المناسب بالإضافة إلى تمييز العنصر المحدد.

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

إذا كان ما تقصده بتعيين ألوان خلفية متعددة هو تعيين لون خلفية مختلف لكل عنصر، فهذا غير ممكن مع ListBox، ولكنه يكون ممكنًا مع ListView، مع شيء مثل:

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

عينة كاملة

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top