开发者

How to change color of selected item in ListBox?

I have a ListBox control with some items开发者_开发技巧 and I want to change color of selected item... How can I do it in C# (WinForms)? Help me please :)


This is how you can set, say Red color, to the selected ASP.NET ListBox items:

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


A good example of this is available here

I am copying over code from the above example here:

"You can set the color of individual items in a ListBox using C# in your .NET WinForm by writting your own handler for the listbox's DrawItem event.

Set the ListBox's DrawMode property:

Add a standard ListBox to your .NET WinForm then set it's DrawMode property to OwnerDrawFixed which forces the ListBox's DrawItem event to be fired.

Write the handler for the DrawItem event:

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

In the InitializeComponent section associate your handler to the DrawItem event:

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


You can try combinations of different brushes and Colors.

using System.Drawing; using 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);
    }


Collating the changes, none of the above answers worked completely for me. Collating what worked for me:

        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;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜