Solved

CComboBox selection

Posted on 1998-09-01
2
1,239 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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