A Color Combo Control
To draw items in different colours in a combo box is a little more involved than the usual subclassing routines that some may be used to. We actually need to capture the creation of the combo control. In order to do this, we need to first subclass our form while it is being created (using the SetWindowsHookEx api) and capture the WM_CREATE message for the combo as it's created. We then need to change the style of the combo to include CBS_OWNERDRAWVARIABLE). Once we have changed the style of the combo as it is created, we can release our existing subclassing.
Now, when the form is loaded, we again need to subclass the form and capture any WM_DRAWITEM for the combo - this allows us to interrupt the painting process for the combo and change the colour of each item as it is drawn.
Be sure to read through the code carefully! You may find that trying to debug the application can cause GPF's in VB (same as any other subclassing example) - so be careful out there!

Comments
request
Posted by surendra on 05/18/2012 09:55amHow to write subquery in vb.net to retrive data multiple tables so pls help me right now if possible
Replyvery good
Posted by Legacy on 08/29/2003 12:00amOriginally posted by: swathi.R
First of all,Thanks for your work.
ReplyAnyway the coding is lengthy which i makes me use the old
combo itself.
Bye
Combo on a Frame
Posted by Legacy on 02/12/2003 12:00amOriginally posted by: Fernando Gomez
I was testing the code in my app, and I can't get it to work when I place the combo over a Frame...
ReplyIs there a way to do it?
thanks.
Button Control
Posted by Legacy on 09/17/2002 12:00amOriginally posted by: Khem keocresna
I would like to know when I take a mouse over the button and I want it changes the background of the button, and when I take mouse out the button I want it changes the background to the original background.
ReplyExcellent but...
Posted by Legacy on 09/17/2002 12:00amOriginally posted by: Kethlan
This contorl is excellent... but i would need the same effect on a listbox. I'm testing with the righttextbox (formating the text...) but isn't very fast...
ReplyHighlighting? (this post is easier to read!)
Posted by Legacy on 08/13/2001 12:00amOriginally posted by: Matthew Heydman
Thank you very much for this clean, useful code. Well
commented and well written- nice job!
I found it useful to comment out the highlight
functionality in the SubClassedForm event because when a
particular color item is highlighted in the combo, the user
cannot discern which color they have chosen.
Also, placing this code in the combo_click event:
Combo1.ForeColor = Combo1.itemData(Combo1.ListIndex)
allows the selected color to be displayed along with the
selected text.
I have a question for you.... Can you think of any way to
remove the highlight from the text after the user selects a
particular item from the list?
SelText is null, and SelStart & Sel End are both zero.
I attempted to use "SendKeys {HOME}" in the combo_click
event- and it works, but occasionally locks up the keyboard
(must get in the way of the subclassing -?)
Is there some flag one can set on the combo style to
prevent any sort of text highlighting?
Thanks again for posting this code, and for any light you
might be able to shed on this issue!
Matthew
ReplyHighlighting?
Posted by Legacy on 08/13/2001 12:00amOriginally posted by: Matthew Heydman
ReplyColored items in combo
Posted by Legacy on 04/02/2001 12:00amOriginally posted by: D
Thanks for the combo box with different colored items.. however, this program crashes vb when i run it. not sure if you know that..
i guess i won't use this in my app!
thanks,
Replydsmith