Click here to Skip to main content
Click here to Skip to main content
Go to top

Hiding a combo's list after selection

, 4 Jul 2002
Rate this:
Please Sign up or sign in to vote.
How to hide a combo's list after selection if the selection starts a long operation

Introduction

If you have an app where you use the selection of an item in a combobox to trigger a possibly long operation, it can be annoying to the user that the combo's list remains visible until the operation is complete. The code shown in this article will hide the list as soon as the user clicks to select a new item in the list.

Use

To use the code you will need to have your own override of CComboBox. I will assume you will already have one, or can make one. In the code the combo class is CMyComboBox.

In your override of CComboBox, add a new virtual function OnChildNotify. Then paste in the code below:

BOOL CMyComboBox::OnChildNotify(UINT message, 
    WPARAM wParam, LPARAM lParam, LRESULT* pLResult) 
{
    if (message == WM_COMMAND && (HWND)lParam == GetSafeHwnd())
    {
        // if we get a SELENDOK then close up the list now, 
        // before letting the app act on it
        if (HIWORD(wParam) == CBN_SELENDOK)
        {
            // if we are a dropdown (ie we have an edit control) 
            // then the edit test doesn't get updated 
            // until after the default handling has finished
            // so we need to explicitly set the edit control's 
            // text to the selection
            if (GetStyle() & CBS_DROPDOWN)
            {
                // get selected text, and set window text so 
                // the combo shows the text just selected
                int nIndex = GetCurSel();
                if (nIndex != -1)
                {
                    CString sText;
                    GetLBText(nIndex, sText);
                    SetWindowText(sText);
                }
            }
            ShowDropDown(FALSE);
        }
    }
    return CComboBox::OnChildNotify(message, 
        wParam, lParam, pLResult);
}

This causes the drop-down list to be hidden when the combo receives a CBN_SELENDOK notification. In addition to closing the list, we also get the selected text and set it as the new window text for the combo. This makes sure that the user sees the newly selected item and not the previously selected item. (This is only necessary if the combo has the CBS_DROPDOWN style, as if it has CBS_DROPDOWNLIST it will update immediately anyway.) That's it!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Paul Vickery
Software Developer (Senior)
United Kingdom United Kingdom
Originally from an electronics background, I moved into software in 1996, partly as a result of being made redundant, and partly because I was very much enjoying the small amount of coding (in-at-the-deep-end-C) that I had been doing!
 
I swiftly moved from C to C++, and learned MFC, and then went on to real-time C on Unix. After this I moved to the company for which I currently work, which specialises in Configuration Management software, and currently program mainly in C/C++, for Windows. I have been gradually moving their legacy C code over to use C++ (with STL, MFC, ATL, and WTL). I have pulled in other technologies (Java, C#, VB, COM, SOAP) where appropriate, especially when integrating with third-party products.
 
In addition to that, I have overseen the technical side of the company website (ASP, VBScript, JavaScript, HTML, CSS), and have also worked closely with colleagues working on other products (Web-based, C#, ASP.NET, SQL, etc).
 
For developing, I mainly use Visual Studio 2010, along with an in-house-designed editor based on Andrei Stcherbatchenko's syntax parsing classes, and various (mostly freeware) tools. For website design, I use Dreaweaver CS3.
 
When not developing software, I enjoy listening to and playing music, playing electric and acoustic guitars and mandolin.
Follow on   Google+   LinkedIn

Comments and Discussions

 
GeneralSample code in project would be helpful. Pinmemberl_d_allan7-Dec-08 2:17 
GeneralA Reasonable Solution... PinmemberBlake Coverett5-Jul-02 15:09 
GeneralRe: A Reasonable Solution... PinmemberPaul S. Vickery7-Jul-02 22:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 5 Jul 2002
Article Copyright 2002 by Paul Vickery
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid