ComboBox Drawing Problem

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
 
T

Tom

Oh... in addition, i notice this is actually only a problem when the
style is not set to DropDownList... and i kind of see why this is, but
anyone know any way round it?
 
K

Ken Tucker [MVP]

Hi,

Private Sub cbo_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles cbo.DrawItem

Dim g As Graphics = e.Graphics

Dim s As String

Dim d As Date

Dim br As Brush = SystemBrushes.WindowText

Dim brBack As Brush

Dim rDraw As Rectangle

Dim bSelected As Boolean = CBool(e.State And DrawItemState.Selected)

Dim bValue As Boolean = CBool(e.State And DrawItemState.ComboBoxEdit)

rDraw = e.Bounds

rDraw.Inflate(-1, -1)

If bSelected And Not bValue Then

brBack = Brushes.LightBlue

g.FillRectangle(Brushes.LightBlue, rDraw)

g.DrawRectangle(Pens.Blue, rDraw)

Else

brBack = Brushes.White

g.FillRectangle(brBack, e.Bounds)

End If

br = Nothing

brBack = Nothing

rDraw = Nothing

Try

s = cbo.Items.Item(e.Index).ToString

Catch

s = ""

End Try

Dim x, y As Integer

x = e.Bounds.Left + 25

y = e.Bounds.Top + 1

Dim c As Color

Dim b As SolidBrush

c = Color.FromName(s)

b = New SolidBrush(c)

g.FillRectangle(b, x - 20, y + 2, 10, 10)

g.DrawString(s, cbo.Font, Brushes.Black, x, y)

End Sub



Ken

---------------------
 
T

Tom

Ken

Thanks, but that is no different to what i am doing. Again it works
fine on an instance of a combo box, however i am creating a custom
control that inherits from the combobox. In this situation the
DrawItem event does not get fired by the selected item.

I believe i have to somehow override the OnPaint method, but this does
not appear to be straight forward for ComboBoxes.

Thanks

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top