如何更改ListBox中所选项目的颜色?

时间:2011-02-21 11:18:35

标签: c# winforms

我有一个带有一些项目的ListBox控件,我想改变所选项目的颜色......我怎样才能在C#(WinForms)中做到这一点? 请帮帮我:)。

4 个答案:

答案 0 :(得分:2)

这是您可以设置所选的ASP.NET ListBox项目的方式,例如红色:

 <asp:ListBox runat="server" ID="ListBox1">
    <asp:ListItem Text="Text1"></asp:ListItem>
    <asp:ListItem Text="Text2"></asp:ListItem>
</asp:ListBox>


        if(ListBox1.SelectedItem != null)
            ListBox1.SelectedItem.Attributes["style"] = "color:red";

答案 1 :(得分:1)

有一个很好的例子here

我正在复制上面示例中的代码:

“您可以通过为列表框的DrawItem事件编写自己的处理程序,在.NET WinForm中使用C#设置ListBox中各个项目的颜色。

设置ListBox的DrawMode属性:

将标准ListBox添加到.NET WinForm,然后将其DrawMode属性设置为OwnerDrawFixed,这会强制触发ListBox的DrawItem事件。

编写DrawItem事件的处理程序:

private void lstBox_DrawItem(object sender, _
          System.Windows.Forms.DrawItemEventArgs e)
{
    //
    // Draw the background of the ListBox control for each item.
    // Create a new Brush and initialize to a Black colored brush
    // by default.
    //
    e.DrawBackground();
    Brush myBrush = Brushes.Black;
    //
    // Determine the color of the brush to draw each item based on 
    // the index of the item to draw.
    //
    switch (e.Index)
    {
        case 0:
            myBrush = Brushes.Red;
            break;
        case 1:
            myBrush = Brushes.Orange;
            break;
        case 2:
            myBrush = Brushes.Purple;
            break;
    }
    //
    // Draw the current item text based on the current 
    // Font and the custom brush settings.
    //
    e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(), 
        e.Font, myBrush,e.Bounds,StringFormat.GenericDefault);
    //
    // If the ListBox has focus, draw a focus rectangle 
    // around the selected item.
    //
    e.DrawFocusRectangle();
}

InitializeComponent部分,将您的处理程序与DrawItem事件相关联:

this.lstBox.DrawItem += 
        new System.Windows.Forms.DrawItemEventHandler(this.lstBox_DrawItem);

答案 2 :(得分:0)

您可以尝试不同画笔和颜色的组合。

使用System.Drawing; 使用System.Drawing.Drawing2D;

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Rectangle rc = listBox1.GetItemRectangle(listBox1.SelectedIndex);
        LinearGradientBrush brush = new LinearGradientBrush(
            rc, Color.Transparent, Color.Red, LinearGradientMode.ForwardDiagonal);
        Graphics g = Graphics.FromHwnd(listBox1.Handle);

        g.FillRectangle(brush, rc);
    }

答案 3 :(得分:0)

整理这些更改,以上所有答案对我来说都不完全有效。整理对我有用的东西:

        public CustomListBox()
    {

        this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
        this.DrawItem += new System.Windows.Forms.DrawItemEventHandler(custom_DrawItem);

        this.MeasureItem += lstMeasureItem;
    }

    protected override void OnSelectedIndexChanged(EventArgs e)
    {
        this.Invalidate();
    }

    private void custom_DrawItem(object sender, DrawItemEventArgs e)
    {
        if (e.Index < 0) return;
        //if the item state is selected then change the back color 
        if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            e = new DrawItemEventArgs(e.Graphics,
                                      e.Font,
                                      e.Bounds,
                                      e.Index,
                                      e.State ^ DrawItemState.Selected,
                                      e.ForeColor,
                                      Color.Gray);//Choose the color

        // Draw the background of the ListBox control for each item.
        e.DrawBackground();

        System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(this.tbForeColor);

        // Draw the current item text
        e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
        // If the ListBox has focus, draw a focus rectangle around the selected item.
        e.DrawFocusRectangle();
    }
    private void lstMeasureItem(object sender, MeasureItemEventArgs e)
    {
        // Cast the sender object back to ListBox type.
        CustomListBox listBox = (CustomListBox)sender;
        e.ItemHeight = listBox.Font.Height;
    }