TIP: Changing the Selected Item's Backcolor in a ListBox

Recently, I ran across an issue where I needed to control the background color of the selected items in a Windows Forms ListBox control. Imagine my surprise when I discovered that not only was such a capability not present in the properties, but that the authors of the control seemed to go out of their way to make it difficult to do so. It may be that they had valid reasons for doing it this way, but I fail to see them. That being said, this is the solution I found. It is simple and straightforward, but took a little time to track it down.

I realized that I had to get involved in the item drawing process, so the first thing was to set the control to Owner drawn and write the event handler for the DrawItem event. I won't go into the details of writing a DrawItem event handler because that is topic is amply covered by others, so I'll simply show the lines that need to be placed at the start of the DrawItem method. My first thought was that I could simply test to see whetherf it was a selected item and instruct the DrawBackground method to use my BackColor if true.

Well, problem number one! The DrawBackground() method of the DrawItemEventArgs takes no parameters and there are no overrides. Hmmm, this means that it is using the BackColor property of the DrawItemEventArgs object.

Okay, let me change that... Whoops, no dice. They made it readonly for no apparent reason. I'll step around that issue by creating a new DrawItemEventArgs using the original's properties and changing the BackColor in the constructor. So far, so good...

if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
   e = new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index,
                             e.State, e.ForeColor, Color.Red);

Nope, it's still drawing using the 'Highlight' named color. Apparently, the DrawBackground method is ignoring the BackColor property when it's a selected item, even though the control does set the BackColor to 'HighLight' for selected item(s)...

Okay, I verified that if it's not a selected item, the above code changes the BackColor as hoped, so now I need to fool the DrawBackground() method so it doesn't see an item as selected.

if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
   e = new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index,
                             e.State ^ DrawItemState.Selected,
                             e.ForeColor, Color.Red);

Success!!!!

By XORing(^) the current State bitmask with the DrawItemState.Selected flag, I have fooled the DrawBackground() method into thinking that it is a normal item and it uses the Backcolor I specified.

I hope this helps.



About the Author

Eric Latham

Began developing in C on a Tandy Color Computer in 1978 (good old 6809E processer, those were the days :) ) which I had to hand solder to get 16k ram. Took up C++ and then C# in turn. Been developing business apps for various companies for the last 20+ years.

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 16, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you starting an on-premise-to-cloud data migration project? Have you thought about how much space you might need for your online platform or how to handle data that might be related to users who no longer exist? If these questions or any other concerns have been plaguing you about your migration project, check out this eSeminar. Join our speakers Betsy Bilhorn, VP, Product Management at Scribe, Mike Virnig, PowerSucess Manager and Michele …

  • Not long ago, security was viewed as one of the biggest obstacles to widespread adoption of cloud-based deployments for enterprise software solutions. However, the combination of advancing technology and an increasing variety of threats that companies must guard against is rapidly turning the tide. Cloud vendors typically offer a much higher level of data center and virtual system security than most organizations can or will build out on their own. Read this white paper to learn the five ways that cloud …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds