Results 1 to 8 of 8

Thread: [RESOLVED] Combobox Draw Item Issue

  1. #1

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    [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
    Last edited by neef; Aug 15th, 2018 at 09:55 PM.
    Intermediate Level Programmer Extraordinaire

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Combobox Draw Item Issue

    From the documentation for the DrawItemEventArgs.Index property:
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    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.
    Intermediate Level Programmer Extraordinaire

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Combobox Draw Item Issue

    Quote Originally Posted by neef View Post
    I should have found it myself!
    The thought did cross my mind.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    432

    Re: Combobox Draw Item Issue

    A value of -1 means nothing is selected.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Combobox Draw Item Issue

    Quote Originally Posted by tommytwotrain View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    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!
    Intermediate Level Programmer Extraordinaire

  8. #8
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    432

    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 .


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width