troubleshooting Question

windows form DrawMode property

Avatar of mpdillon
mpdillon asked on
.NET Programming
2 Comments1 Solution330 ViewsLast Modified:
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
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
            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

        ' change brush color if item is selected
        If e.State = Windows.Forms.DrawItemState.Selected Then
            ComboBrush = Brushes.White
            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

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros