Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CComboBox selection

Posted on 1998-09-01
2
Medium Priority
?
1,318 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 200 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

877 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