c# – 如何更改ListBox选择背景颜色?

前端之家收集整理的这篇文章主要介绍了c# – 如何更改ListBox选择背景颜色?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
默认情况下,使用默认颜色的 Windows设置是蓝色的.
假设我想把它永久改成红色.我使用Winforms.

提前致谢.

解决方法

您必须覆盖 Drawitem事件并将 DrawMode属性设置为 DrawMode.OwnerDrawFixed

检查这个样本

private void listBox1_DrawItem(object sender,DrawItemEventArgs e)
{
    if (e.Index<0) return;
    //if the item state is selected them 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.Yellow);//Choose the color

    // Draw the background of the ListBox control for each item.
    e.DrawBackground();
    // Draw the current item text
    e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),Brushes.Black,StringFormat.GenericDefault);
    // If the ListBox has focus,draw a focus rectangle around the selected item.
    e.DrawFocusRectangle();
}
原文链接:https://www.f2er.com/csharp/94823.html

猜你在找的C#相关文章