Hi-liting/Shading a string in a CListBox?

I have derived a class from CListBox and made it owner draw.  I have also updated the DrawItem() function to draw the strings that are added to the list box.  Now my problem is, once the user double-clicks on the string I'd like to have it remain shaded gray.  How do I do this? Help!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tommy HuiEngineerCommented:
What exactly do you mean by remain shaded gray? Do you mean as if it wasn't selected? Or if it is selected?

Regardless, the code you have for owner-draw determines exactly how the items are to be displayed. I suspect you probably have code in there to check if the state is selected or focused and to draw something different. One of those can be combined to achieve the effect you want.

For example, if you want it to appear selected whether the listbox has focus or not, then remove the check for the focused state because that is no longer needed.

Note that you can also change the selected state of the item when the user double clicks on it. If you don't want it to be selected after double clicking, then add the code to turn off the selection.
tdubroffAuthor Commented:
I have a list box that is filled with several strings.  Once the user double-clicks on each item, then that item gets selected and is displayed with a blue background.  When the user selects a different item, then I want the original item to be displayed with a gray background (meaning that it has been selected before).
OK .. in your derived class, associate some data with each listbox entry.  This data will (include) a flag to say "I have been selected".  In your owner-draw code, look at the associated data for the item you are drawing and set the colours appropriately.

Check out the source code for the MFC class CCheckListBox which uses an ownerdran listbox to show (and respond to) a check box on each line.  This also associates a flag with each item for its check state and draws the checkbox accordingly.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

tdubroffAuthor Commented:
Thanks RONSLOW.  Once again I have gotten something working using your suggestions.  However my problem is not quite done.  The user can if he/she wants press a button which will un-gray all grayed items.  The question is then, how do I cause a refresh of the list box (how do I force DrawItem to be called)?  
Invalidate() it.

tdubroffAuthor Commented:
Thanks a bunch!
Don't you just love two-word answers - short and to the point :-).

Glad I could help !!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.