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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
set up trigger (or something) to rename files (starting with ATL) in a linux directory 6 55
List out all word 7 332
matchUp  challenge 6 76
unix example issues 18 106
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

713 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