CComboBox selection

Posted on 1998-09-01
Last Modified: 2013-11-20

Im trying to cause my ComboBox select the list entry
which corresponds to the one the user types.
The problem is that after the entry is selected,
it is somehow deselected.
Im doing this during the function OnEditupdate.
MFC probably calls some other functions after OnEditUpdate.
How can i solve it ?

Im using a simple ComboBox in Visual C++ 5, NT 4.

void CMyDlg::OnEditupdateCombo1()
        CComboBox* pCombo  =(CComboBox*)GetDlgItem(IDC_COMBO1);
        int res = pCombo->FindString(0, m_sCombo);
        if (res != CB_ERR)
                pCombo->SetCurSel(res); //selects, but later

thanks in advance,
Question by:batat
LVL 23

Expert Comment

ID: 1321595

Accepted Solution

Answers2000 earned 100 total points
ID: 1321596
Well there's a couple of problems with this

1. As chensu says you want to capture CBN_EDITCHANGE messages rather CBN_EDITUPDATE messages.

The former occurs when the combo box is about to be update (the update hasn't occurred yet)

The latter occurs when the combo box has been updated [the update has occurred).

Now because you're doing you're update in CBN_EDITUPDATE, the sequence is
i. The user selects an item, say "A"
ii. CBN_EDITUPDATE message occurs indicating "A" is about to be made the combo box selection
iii. Your update occurs, say requesting "B" be made the selected item
iv. Windows makes "A" the selection (continuing from i and ii)
v. Windows sends a CBN_EDITCHANGE message indicating "A" has been made the selection

2. A second problem, which as it happens isn't causing a problem in your program, but is strictly speaking "risky" is
CComboBox* pCombo  =(CComboBox*)GetDlgItem(IDC_COMBO1);

GetDlgItem() returns a CWnd * which can be created from as a temporary CWnd.  Upcasting to a derived class (CComboBox *) is dangerous (I know a lot of books do this, but it's still dangerous).

You get away with this because FindString and SetCurSel are defined:

_AFXWIN_INLINE int CComboBox::FindString(int nStartAfter, LPCTSTR lpszString) const
      { ASSERT(::IsWindow(m_hWnd)); return (int)::SendMessage(m_hWnd, CB_FINDSTRING, nStartAfter,
            (LPARAM)lpszString); }

_AFXWIN_INLINE int CComboBox::SetCurSel(int nSelect)
      { ASSERT(::IsWindow(m_hWnd)); return (int)::SendMessage(m_hWnd, CB_SETCURSEL, nSelect, 0); }

If CComboBox had extra data members (it doesn't) or relied on extra construction (when the CWnd * comes a temporary CWnd object) this would fail, if the 2 functions used depended on this.

Anyway, the better solution would be to add a Control variable (e.g. Member variables tab in Class Wiz) to the class (say m_combo of type CComboBox).

3. Another problem is you might one want to consider using FindStringExact.  FindString searches on the prefix of a string, therefore if you have strings with similar beginning sequences of characters, you'll select the same one.

4. The final problem is the FindString (and FindStringExact) have 0 for the first parameter.  This means the first item in the combo would be searched last (this number is the item *before* where searching starts)

The final solution is therefore

void CMyDlg::OnEditchangeCombo1()
   int res = m_combo.FindStringExact( -1, m_sCombo ) ;

   if ( res != CB_ERR )
       m_combo.SetCurSel(res); // should work


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Expand data scrubbing tool 13 35
method notes when mouse over in eclipse 5 87
haveThree challenge 22 121
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 865
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question