windows form DrawMode property

mpdillon
mpdillon used Ask the Experts™
on
I want to display colors in a ComboBox. I found some code that does that. But I must not be implementing it correctly. Part of the text in my combobox is missing.
This image shows the Combobox with the DrawMode set to Normal. This ignores the color code which is listed at the end.
Normal DrawMode
This next image is with the DrawMode property set to OwnerDrawVariable
 OwnerDrawVariable  
In the code below what is making the second half of the row text disappear?
 
Private Sub cboColor_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles cboColor.DrawItem
        If e.Index < 0 Then
            e.DrawBackground()
            e.DrawFocusRectangle()
            Exit Sub
        End If


        ' Get the Color object from the Items list
        Dim CurrentColor As Color = System.Drawing.Color.FromName(Trim(Mid(Me.cboColor.Items(e.Index).ToString, 1, 20))) 'CType(cboColor.Items(e.Index), Color)

        ' get a square using the bounds height
        'Dim SizeRect As Rectangle = New Rectangle(2, e.Bounds.Top + 2, e.Bounds.Width, e.Bounds.Height - 2)
        'Dim SizeRect As Rectangle = New Rectangle(2, e.Bounds.Top + 2, e.Bounds.Width / 2, e.Bounds.Height - 2)
        Dim SizeRect As Rectangle = New Rectangle(2, e.Bounds.Top + 2, 10, 10)
        Dim ComboBrush As Brush

        ' call these methods first
        e.DrawBackground()
        e.DrawFocusRectangle()

        ' change brush color if item is selected
        If e.State = Windows.Forms.DrawItemState.Selected Then
            ComboBrush = Brushes.White
        Else
            ComboBrush = Brushes.Black
        End If

        ' draw a rectangle and fill it
        e.Graphics.DrawRectangle(New Pen(CurrentColor), SizeRect)
        e.Graphics.FillRectangle(New SolidBrush(CurrentColor), SizeRect)

        ' draw a border
        SizeRect.Inflate(1, 1)
        e.Graphics.DrawRectangle(Pens.Black, SizeRect)

        ' draw the Color name
        e.Graphics.DrawString(CurrentColor.Name, cboColor.Font, ComboBrush, e.Bounds.Height + 5, ((e.Bounds.Height - cboColor.Font.Height) \ 2) + e.Bounds.Top)

    End Sub

    Private Sub cboColor_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles cboColor.MeasureItem
        Dim myRandom As New Random
        e.ItemHeight = myRandom.Next(10, 20)
    End Sub

Open in new window



Thanks,
pat
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2008
Commented:
You are drawing your own text:

e.Graphics.DrawString(CurrentColor.Name, cboColor.Font, ComboBrush, e.Bounds.Height + 5, ((e.Bounds.Height - cboColor.Font.Height) \ 2) + e.Bounds.Top)

Open in new window


That would suggest that you are not drawing all the text.

Author

Commented:
Thanks. I understand now.

pat

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial