c# - 如何更改列表框选择背景颜色?

标签 c# winforms listbox background selection

它似乎使用 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(),e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
    // If the ListBox has focus, draw a focus rectangle around the selected item.
    e.DrawFocusRectangle();
}

alt text

关于c# - 如何更改列表框选择背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3663704/

相关文章:

c# - .GetAwaiter() 和 ConfigureAwait() 的区别

c# - 裁剪图像通用应用程序 C#

c# - 将按钮添加到 DataGridView

wpf - 将列表框选择的项目信息传递给用户控件(WPF)

c# - 列表框显示成员不起作用

c# - WCF:通用接口(interface)的序列化是否可能?

c# - 依赖注入(inject)容器?它有什么作用?

.net - Windows 窗体 : A modal form that gets opened/closed by the application rather than the user?

c# - 如何防止垂直滚动条在控件中占用空间,导致水平滚动条?

c# - 从 Asp.net ListBox 中删除选定的项目