T
Tom
Hi
I have inherited a ComboBox that i am using to list all the available
SystemColors with a little rectangle painted to the left of the name
in the correct color. The ComboBox is set to OwnerDrawVariable and i
am implementing the MeasureItem and DrawItem events:
Private Sub ColorDropDown_MeasureItem(ByVal sender As
System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs)
Handles MyBase.MeasureItem
e.ItemHeight = 13
e.ItemWidth = 170
End Sub
Protected Sub ColorDropDown_DrawItem(ByVal sender As
System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs)
Handles MyBase.DrawItem
Dim ColorName As String = CType(Me.Items(e.Index), String)
Dim ItemColor As New System.Drawing.Color
Dim ColorValue() As String = Split(ColorName, ", ")
If ColorValue.GetLength(0) = 3 Then
ItemColor = Color.FromArgb(CInt(ColorValue(0)),
CInt(ColorValue(1)), CInt(ColorValue(2)))
Else
ItemColor = Color.FromName(ColorName)
End If
' Draw the background of the item.
e.DrawBackground()
Dim EditItem As Integer
If (e.State And DrawItemState.ComboBoxEdit) =
DrawItemState.ComboBoxEdit Then EditItem = 1
' Create a square filled with the item color.
Dim rectangle As rectangle = New rectangle(2 + EditItem,
e.Bounds.Top + 2, 20, e.Bounds.Height - 4)
e.Graphics.FillRectangle(New SolidBrush(ItemColor),
rectangle)
e.Graphics.DrawRectangle(New Pen(Color.FromName("Black")),
rectangle)
' Draw the color name
e.Graphics.DrawString(ColorName, Font,
System.Drawing.Brushes.Black, _
New RectangleF(e.Bounds.X + rectangle.Width + 4 -
(EditItem * 2), e.Bounds.Y + 1, _
e.Bounds.Width, e.Bounds.Height))
' Draw the focus rectangle if the mouse hovers over an
item.
e.DrawFocusRectangle()
End Sub
This works fine or items drawn in the dropdown list, however the
DrawItem is not being called for the currently selected item. It works
fine on a non inherited ComboBox.
Anyone know what's wrong?
Thanks
Tom
I have inherited a ComboBox that i am using to list all the available
SystemColors with a little rectangle painted to the left of the name
in the correct color. The ComboBox is set to OwnerDrawVariable and i
am implementing the MeasureItem and DrawItem events:
Private Sub ColorDropDown_MeasureItem(ByVal sender As
System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs)
Handles MyBase.MeasureItem
e.ItemHeight = 13
e.ItemWidth = 170
End Sub
Protected Sub ColorDropDown_DrawItem(ByVal sender As
System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs)
Handles MyBase.DrawItem
Dim ColorName As String = CType(Me.Items(e.Index), String)
Dim ItemColor As New System.Drawing.Color
Dim ColorValue() As String = Split(ColorName, ", ")
If ColorValue.GetLength(0) = 3 Then
ItemColor = Color.FromArgb(CInt(ColorValue(0)),
CInt(ColorValue(1)), CInt(ColorValue(2)))
Else
ItemColor = Color.FromName(ColorName)
End If
' Draw the background of the item.
e.DrawBackground()
Dim EditItem As Integer
If (e.State And DrawItemState.ComboBoxEdit) =
DrawItemState.ComboBoxEdit Then EditItem = 1
' Create a square filled with the item color.
Dim rectangle As rectangle = New rectangle(2 + EditItem,
e.Bounds.Top + 2, 20, e.Bounds.Height - 4)
e.Graphics.FillRectangle(New SolidBrush(ItemColor),
rectangle)
e.Graphics.DrawRectangle(New Pen(Color.FromName("Black")),
rectangle)
' Draw the color name
e.Graphics.DrawString(ColorName, Font,
System.Drawing.Brushes.Black, _
New RectangleF(e.Bounds.X + rectangle.Width + 4 -
(EditItem * 2), e.Bounds.Y + 1, _
e.Bounds.Width, e.Bounds.Height))
' Draw the focus rectangle if the mouse hovers over an
item.
e.DrawFocusRectangle()
End Sub
This works fine or items drawn in the dropdown list, however the
DrawItem is not being called for the currently selected item. It works
fine on a non inherited ComboBox.
Anyone know what's wrong?
Thanks
Tom