c# - 单击项目时,如何删除包裹 listBox1 中所选项目的蓝色?

标签 c# winforms

<分区>

当我单击 listBox1 以选择一个项目时,该项目周围保持蓝色。我怎样才能去除这种颜色?

enter image description here

最佳答案

使用此代码将选择颜色更改为您想要的任何颜色:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        //Add this to your form initialization
        this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);
    }

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

我已经使用透明作为颜色来移除选择的背景颜色,但如果您想要任何其他颜色,只需自己更改即可。
我使用这个答案作为帮助:How to change ListBox selection background color?

在表单上显示:

ListBox Form

如果你想取消选择列表框中的项目,使用任一个

listBox1.ClearSelected();

listBox1.SelectedIndex = -1;

干杯!

关于c# - 单击项目时,如何删除包裹 listBox1 中所选项目的蓝色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23742503/

相关文章:

c# - .net core 和 .net framework 4.6 的 Nuget 包兼容性

c# - 检测浏览器中是否弹出打开文件对话框

c# - 与 UIAutomationCore 相比,System.Windows.Automation 在枚举表行时非常慢

wpf - 如何以编程方式在 WinForm 应用程序中创建 WPF 窗口

c# - 桌面上的透明表格

c# - 计算量大的粒子系统用什么技术?

c# - EntityFramework Include (Eager Load) 虚拟属性的虚拟属性

c# - asp net MVC3 回发时提交对象列表为空

C# 在窗体之间传递多维数组

c# - 检查一个点是否在旋转的矩形中(C#)