簡體   English   中英

如何更改列表框中所選項目的顏色?

[英]How to change color of selected item in ListBox?

我有一個包含一些項目的 ListBox 控件,我想更改所選項目的顏色......我如何在 C# (WinForms) 中做到這一點? 請幫幫我 :)

這是您可以為選定的 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";

一個很好的例子可以在這里找到

我在這里復制上面示例中的代碼:

“通過為列表框的DrawItem事件編寫自己的處理程序,您可以在 .NET WinForm 中使用 C# 設置列表框中各個項目的顏色。

設置 ListBox 的 DrawMode 屬性:

向您的 .NET WinForm 添加一個標准 ListBox,然后將它的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);

您可以嘗試不同畫筆和顏色的組合。

使用 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);
    }

整理這些變化,以上答案都不適合我。 整理對我有用的東西:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM