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

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds