How to change combobox background color (not just the drop down list part)

后端 未结 6 1521
走了就别回头了
走了就别回头了 2020-12-15 17:44

In a winform application running on windows 7 I want the change the background color of a combobox to highlight it. The comboxbox has a DropDownStyle of DropDownList.

<
6条回答
  •  自闭症患者
    2020-12-15 17:45

    This should get you started.

    Change the combobox DrawMode property to OwnerDrawFixed, and handle the DrawItem event:

    private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        int index = e.Index >= 0 ? e.Index : 0;
        var brush = Brushes.Black;
        e.DrawBackground();
        e.Graphics.DrawString(comboBox1.Items[index].ToString(), e.Font, brush, e.Bounds, StringFormat.GenericDefault);
        e.DrawFocusRectangle();
    }
    

    The background color will be right but the style of the box will be flat, not the usual 3D style.

提交回复
热议问题