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

后端 未结 6 1535
走了就别回头了
走了就别回头了 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:52

    Here is what i used for a vb project for another beginner that might be interested. One can use the sender of the event as the combobox that triggered it. By casting it, you can have access to the elements of the list. I also changed the graphics TextRenderingHint for better font display.

    Private Sub PaintComboBoxItem(sender As Object, e As DrawItemEventArgs)
        Dim combobox As ComboBox = sender
        Dim index As Integer = If(e.Index >= 0, e.Index, 0)
        Dim brush As Brush = If(combobox.Enabled,
                                    New SolidBrush(m_UITheme.TitleColor),
                                    New SolidBrush(m_UITheme.White))
        e.Graphics.SmoothingMode = SmoothingMode.HighQuality
        e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit
        e.DrawBackground()
        e.Graphics.DrawString(combobox.Items(index).ToString(), combobox.Font, brush, e.Bounds, StringFormat.GenericDefault)
        e.DrawFocusRectangle()
    End Sub
    

提交回复
热议问题