Owner Drawn Combobox OnDrawItem()

I have an owner drawn combobox.  When dropped down the FocusRectangle highlights an item in the combobox... however with the owner drawn control the text under the FocusRectangle is still the original color and therefore cannot be seen well.  In order to get the color to change under the highlight there needs to be a test to see if this is the highlighted item...   If Me.SelectedIndex = e.Index Then ... so using this i change the brush color for the drawstring method.  

Protected Overrides Sub OnDrawItem(ByVal e As DrawItemEventArgs)
        Dim brsh As Brush

            If Me.SelectedIndex = e.Index Then
                brsh = Brushes.Red
                brsh = Brushes.Black
            End If

            e.Graphics.DrawString(CType(ARList(e.Index), String), Me.Font, brsh, _
                    New RectangleF(0, e.Bounds.Y, _
                    e.Bounds.Width, e.Bounds.Height))
end sub

this works to change the color of the text under the focusrectangle however when you move the rectangle elsewhere the next item's color is changed without the original being changed back.  It was suggested to me to use a loop to redraw all of the controls but that is lost on me because you cannot change the e.index which is the index of the current item that is being drawn. So ultimately i need a way to redraw all rows of the combobox on every call of ondrawitem()

a correct answer here also gets half of the points at http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_21093096.html
Who is Participating?
Call back the draw method of the whole combobox prior to call change the color of the item
Forgot to mention, you can redraw by calling the method refresh.
Bob LearnedCommented:
You could also force a redraw by Invalidating the entire control, which should force a Paint, and then each OnDrawItem would need to be called for each item.

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

BangorCCAuthor Commented:
im not sure if i understand what you are saying do you mean add combobox.Refresh() somewhere in the code above?  That would not work i have already tried it.  What method are you talking about when you say the draw method for the whole combobox.
Bob LearnedCommented:
Something like Me.Invalidate

You to call a combobox.Refresh() before its calls your OnDrawItem()
, all you items will then redraw.

By calling your Refresh it should call OnDrawItem for each item in your list.
BangorCCAuthor Commented:
ok the invalidate and refresh dont seem to work when i add them to the top of the OnDrawItem sub... I cannot invalidate or refresh outside of this sub because all of my problems occur when the combobox is dropped down before an item is actually selected.  Everything is fine when you click the dropdown

here is what is happening in sequence
when you first dropdown the combobox you get this

then you move the selection down to the next item in the list

and again

interestingly the OnDrawItem fires for the item you are leaving however e.index is still pointing to it at that point so the test fails
Can you put up some kind for flag, which will almost follow the e.index, but will be accurate for you?

And ar eyou from Bangor Maine?
BangorCCAuthor Commented:
DJ_Back-Q   I am from Bangor ME are you local?
Well I cannot think of a way to keep a flag without having a different sub fire when the selection changes in the dropdown... Otherwise my flag will keep loading the same values that i am trying to use now
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.