Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8440885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:25:20+00:00 2026-06-10T08:25:20+00:00

I need to show items in a combobox with a different background color. I

  • 0

I need to show items in a combobox with a different background color. I also want to change what that color is depending on if the item is selected (or the mouse is on top of it), just the same way it works when a combobox is not owner-drawn.

It is all working fine, except that when the mouse comes off one of the items that I changed the color for, the item keeps the same color as when the mouse was on top. In the example below, the item ‘other’ is initially correctly drawn with myUnselectedBrush; the mouse goes over top, it is correctly drawn with mySelectedBrush; when the mouse comes off, it is incorrectly still drawn with mySelectedBrush; it should have been drawn with myUnselectedBrush. Everything works fine for item ‘something’, whose color is not altered.

What am I doing wrong?

private void comboBoxDraw(object sender, DrawItemEventArgs e)
{
    ComboBox cb = (ComboBox)sender;
    Graphics g = e.Graphics;

    e.DrawBackground();
    if (e.Index > -1)
    {
        object item = cb.Items[e.Index];
        switch (somethingOrOther)
            {
                case something:
                    break;

                case other:
                    e.Graphics.FillRectangle(
                               (cb.SelectedIndex == e.Index) 
                                   ? mySelectedBrush 
                                   : myUnselectedBrush, 
                               e.Bounds);
                    break;
            }
        }
    }

    e.DrawFocusRectangle();
    if (e.Index > -1)
    {
       // draw the string
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:25:21+00:00Added an answer on June 10, 2026 at 8:25 am

    Rather than using

    cb.SelectedIndex == e.Index
    

    I needed to use DrawItemState:

    ((state & DrawItemState.Selected) > 0) || ((state & DrawItemState.HotLight) > 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to show non available dates in different color based on the event
I need to be able to show the items that has the top 10
On click of certain listview items, I need to show sub item under the
I need some help programatically setting the selected item in a combobox. I've got
I need to show item id in a href tag ,but it must not
I need to show a div positioned relative to where the link is that
I need to realize a WebSite with a grid that shows all items of
What I need to accomplish is a ComboBox that shows People. When you expand
I have an unordered list of 50 items. I want to show only 10
I am trying to create a web-part to show items that relate to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.