c# - 将鼠标悬停在组合框项目上时更改组合框项目的背景颜色

标签 c# forms combobox

我有一个 ComboBox,我修改成这样:

enter image description here

它的代码是这样的(cat_color 是一个包含像“#7FFFD4”这样的字符串的数组):

private void cboCategory_DrawItem(object sender, DrawItemEventArgs e)
{
    if (e.Index != -1)
    {
        e.DrawBackground();
        e.Graphics.FillRectangle(new SolidBrush(ColorTranslator.FromHtml(cat_color1[e.Index])), e.Bounds);
        Font f = cboCategory.Font;
        e.Graphics.DrawString(cboCategory.Items[e.Index].ToString(), f, new SolidBrush(ColorTranslator.FromHtml(cat_color2[e.Index])), e.Bounds, StringFormat.GenericDefault);
        e.DrawFocusRectangle();
    }
}

我现在的目标是当我将鼠标悬停在项目上时更改项目的背景颜色。这可能吗?

最佳答案

这将为您完成这项工作。

if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
    e.DrawBackground();
    e.Graphics.FillRectangle(new SolidBrush(SystemColors.Highlight), e.Bounds);
    Font f = cboCategory.Font;
    e.Graphics.DrawString(cboCategory.Items[e.Index].ToString(), f, new SolidBrush(ColorTranslator.FromHtml(cat_color2[e.Index])), e.Bounds, StringFormat.GenericDefault);
    e.DrawFocusRectangle();
}

关于c# - 将鼠标悬停在组合框项目上时更改组合框项目的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26808118/

相关文章:

C# 计时器不会滴答作响

c# - 在 IE7 中启用 C# 清除 cookie

c# - 线程池和委托(delegate)操作 - WaitHandle.WaitAll()

c# - 无法构建 JSIL

javascript - 无法使用 javascript 获取表单的值

Symfony2 的 CSS 形式 : "Hello World" equivalent

java - 如何将组合框选定的项目pk插入mysql?

php脚本不会抛出异常

combobox - JavaFX - 如何使 ComboBox hgrow?

c# - 如何创建尽可能多的相同数据绑定(bind)组合框,但从相同的字段名称中提取不同的值?