Avatar of mpdillon
mpdillon
 asked on

windows form DrawMode property

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
.NET Programming

Avatar of undefined
Last Comment
mpdillon

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Bob Learned

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
mpdillon

ASKER
Thanks. I understand now.

pat
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck