Solved

CComboBox selection

Posted on 1998-09-01
2
1,222 Views
Last Modified: 2013-11-20
Hi,

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()
{
        UpdateData(TRUE);
        CComboBox* pCombo  =(CComboBox*)GetDlgItem(IDC_COMBO1);
        int res = pCombo->FindString(0, m_sCombo);
        if (res != CB_ERR)
                pCombo->SetCurSel(res); //selects, but later
deselects
       
}


thanks in advance,
Avi.
0
Comment
Question by:batat
2 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 1321595
Try ON_CBN_EDITCHANGE instead of ON_CBN_EDITUPDATE.
0
 
LVL 8

Accepted Solution

by:
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()
{
   UpdateData(TRUE);
   int res = m_combo.FindStringExact( -1, m_sCombo ) ;

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

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now