[RESOLVED] Combobox Draw Item Issue
I'm trying to make certain items in a combobox a different color using the DrawItem event. I've done this many times before but in this instance I keep getting a value of -1 for the e.index variable.
Isn't DrawItem called when there actually is at least one item in the combobox? A value of -1 for e.index means it's empty (which it shouldn't be in my case because I added the items in the forms Load event). I set ItemDraw mode to OwnerFixed. Am I missing some other setting?
Code:
Private Sub cbCollege_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles cbCollege.DrawItem
e.DrawBackground()
Dim myForeColor As Color = Color.Black
Dim myBrush As Brush = New SolidBrush(Color.White)
If 'Test for color change
myForeColor = Color.Blue
End if
e.Graphics.FillRectangle(myBrush, e.Bounds)
myBrush = New SolidBrush(myForeColor)
e.Graphics.DrawString(sender.Items(e.Index).ToString(), New Font("Calibri", 12, FontStyle.Bold), myBrush, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
e.DrawFocusRectangle()
End Sub
Re: Combobox Draw Item Issue
From the documentation for the DrawItemEventArgs.Index property:
Quote:
This property returns the Item value of the item being drawn in the System.Windows.Forms.Control.ControlCollection. This property can return -1 if items were removed from the list.
Without having tested, my guess is that you should either just ignore cases where it's -1 or just accept all the default drawing. You could test both and see what happens in each case.
Re: Combobox Draw Item Issue
Thank you for finding that info. I should have found it myself!
This points me in the right direction for possibly debugging this as I do add and remove items on the list based on other variables.
Re: Combobox Draw Item Issue
Quote:
Originally Posted by
neef
I should have found it myself!
The thought did cross my mind. ;)
Re: Combobox Draw Item Issue
A value of -1 means nothing is selected.
Re: Combobox Draw Item Issue
Quote:
Originally Posted by
tommytwotrain
A value of -1 means nothing is selected.
I think that you may be thinking of the SelectedIndex of the ComboBox itself. The issue here was that e.Index was -1 inside the DrawItem event handler. That e.Index is supposed to be the index of the item being drawn and items will need to be drawn whether any of them are selected or not. It's the e.State property that indicates, amongst other things, whether the item being drawn is selected or not, so that you know how to draw it.
Without having investigated this at all, my guess would be that, when you remove an item, there is one more DrawItem event raised for that item and, on that occasion, e.Index is -1. Maybe they wanted to give you a chance to do some cleanup for that item or maybe this is just incidental behaviour that they just didn't bother to prevent.
Re: Combobox Draw Item Issue
One line of code at the beginning of the DrawItem event fixed the issue just like jmc suggested:
if e.index = -1 then return
Moving items in and out of the combobox indeed caused this
Thank you!
Re: Combobox Draw Item Issue
[QUOTE=jmcilhinney;5310123]I think that you may be thinking of the SelectedIndex of the ComboBox itself.
jmc,
Oh yes that is correct. I was confused. OP means the draw event e.index not the control.selectedindex .
:)