A Color Combo Control

screen-shot

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!

Download zipped project and exe (10k)



Comments

  • request

    Posted by surendra on 05/18/2012 09:55am

    How to write subquery in vb.net to retrive data multiple tables so pls help me right now if possible

    Reply
  • very good

    Posted by Legacy on 08/29/2003 12:00am

    Originally posted by: swathi.R

    First of all,Thanks for your work.
    Anyway the coding is lengthy which i makes me use the old
    combo itself.
    Bye

    Reply
  • Combo on a Frame

    Posted by Legacy on 02/12/2003 12:00am

    Originally 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...

    Is there a way to do it?
    thanks.

    Reply
  • Button Control

    Posted by Legacy on 09/17/2002 12:00am

    Originally 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.

    Reply
  • Excellent but...

    Posted by Legacy on 09/17/2002 12:00am

    Originally 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...

    Reply
  • Highlighting? (this post is easier to read!)

    Posted by Legacy on 08/13/2001 12:00am

    Originally 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


    Reply
  • Highlighting?

    Posted by Legacy on 08/13/2001 12:00am

    Originally 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


    Reply
  • Colored items in combo

    Posted by Legacy on 04/02/2001 12:00am

    Originally 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,
    dsmith

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

Top White Papers and Webcasts

  • Instead of only managing projects organizations do need to manage value! "Doing the right things" and "doing things right" are the essential ingredients for successful software and systems delivery. Unfortunately, with distributed delivery spanning multiple disciplines, geographies and time zones, many organizations struggle with teams working in silos, broken lines of communication, lack of collaboration, inadequate traceability, and poor project visibility. This often results in organizations "doing the …

  • In its latest email archiving report, Info-Tech Research Group acknowledged CommVault for its "holistic content archiving solution" that offers extensive feature functionality for email archiving. Read the Info-Tech Email Archiving Vendor Landscape and learn why CommVault ranks as a 'Champion.'

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds