I'm trying to turn this into a UserControl for a project and my end goal is to have a combobox that lists all of the colors in the color enum and it also shows a little box next to each item in the drop down that shows the color of each color's name. Just like the one in VS 2005 when you click Tools -> Options -> Fonts & Colors, there's an "Item Foreground" and "Item Background" that does this.
I already have the drawing of the colors and whatnot done, here's the code:
This fills the colors in the combo box itself
This handles the DrawItem stuff:
This all works, however when you highlight the items in the combo box, the item's text remains black. I would like it if the item's text turns white (like how VS 2005 does) and when you move the mouse over another item, it turns the new item white and returns the old item back to black.
Any suggestions?
I already have the drawing of the colors and whatnot done, here's the code:
This fills the colors in the combo box itself
VB.NET:
Dim CurrColor As Color
For Each aColorName As String In System.Enum.GetNames(GetType(System.Drawing.KnownColor))
CurrColor = Color.FromName(aColorName)
If CurrColor.IsSystemColor = False Then NumerPanelColorComboBox.Items.Add(CurrColor)
Next aColorName
VB.NET:
Private Sub ComboBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles NumerPanelColorComboBox.DrawItem
e.DrawBackground()
e.DrawFocusRectangle()
If e.Index >= 0 Then
' Get the Color object from the Items list
Dim aColor As Color = CType(CType(sender, ComboBox).Items(e.Index), Color)
' get a square using the bounds height
Dim rect As Rectangle = New Rectangle(2, e.Bounds.Top + 2, e.Bounds.Height, e.Bounds.Height - 5)
Dim br As Brush = Brushes.White
' call these methods first
e.DrawBackground()
e.DrawFocusRectangle()
' change brush color if item is selected
If e.State = DrawItemState.Selected OrElse e.State = DrawItemState.ComboBoxEdit Then
br = Brushes.White
Else
br = Brushes.Black
End If
' draw a rectangle and fill it
e.Graphics.DrawRectangle(New Pen(aColor), rect)
e.Graphics.FillRectangle(New SolidBrush(aColor), rect)
' draw a border
rect.Inflate(1, 1)
e.Graphics.DrawRectangle(Pens.Black, rect)
' draw the Color name
e.Graphics.DrawString(aColor.Name, CType(sender, ComboBox).Font, br, e.Bounds.Height + 5, ((e.Bounds.Height - CType(sender, ComboBox).Font.Height) \ 2) + e.Bounds.Top)
End If
End Sub
Any suggestions?