Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1327
  • Last Modified:

How to insert a text in the Edit box of a ComboBox?

How to insert a text in the Edit box of a ComboBox without adding it to the list in the combobox? Anyone?
0
KeithTeo
Asked:
KeithTeo
2 Solutions
 
GloriousRainCommented:
Hi KeithTeo,
You can subclass CEdit inside CComboBox, then process Edit box. Below article shows how to do that
 
<MSDN>
HOWTO: Subclass CListBox and CEdit Inside of CComboBox

Q174667


--------------------------------------------------------------------------------
The information in this article applies to:

The Microsoft Foundation Classes (MFC), used with:
Microsoft Visual C++, 32-bit Editions, versions 4.0, 4.1, 4.2, 5.0, 6.0

--------------------------------------------------------------------------------


SUMMARY
While it is simple to directly subclass a combo box control, it is not simple to subclass the edit or list box inside a combo box. The problem is that it is difficult to get the HWNDs of the child controls in a portable manner.

One safe way to subclass the internal edit and list box controls is to subclass them in the WM_CTLCOLORXXX messages. Because Win32 sends separate WM_CTLCOLOREDIT and WM_CTLCOLORLISTBOX messages, these messages are safe and easy ways to get the HWNDs of the child controls of the combo box.

Below is a CSuperComboBox class, which is an MFC implementation of this method. Because MFC routes all the WM_CTLCOLOR messages to OnCtlColor, the subclassing takes place there.



MORE INFORMATION
Use ClassWizard to derive a class from CComboBox and add message handlers for WM_CTLCOLOR and WM_DESTROY. Then manually edit the header file to add the data members, m_edit and m_listbox. Finally, copy the code from the message handlers below:

Sample Code

   // SuperComboBox.h : header file
   class CSuperComboBox : public CComboBox
   {
   public:
      CEdit      m_edit;
      CListBox   m_listbox;
   protected:
      afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
      afx_msg void OnDestroy();
   ...
   };

   // SuperComboBox.cpp : implementation file
   HBRUSH CSuperComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
   {
      if (nCtlColor == CTLCOLOR_EDIT)
      {
         //[ASCII 160][ASCII 160][ASCII 160]Edit control
         if (m_edit.GetSafeHwnd() == NULL)
            m_edit.SubclassWindow(pWnd-&gt;GetSafeHwnd());
      }
      else if (nCtlColor == CTLCOLOR_LISTBOX)
      {
         //ListBox control
         if (m_listbox.GetSafeHwnd() == NULL)
            m_listbox.SubclassWindow(pWnd-&gt;GetSafeHwnd());
      }
      HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
      return hbr;
   }

   void CSuperComboBox::OnDestroy()
   {
      if (m_edit.GetSafeHwnd() != NULL)
         m_edit.UnsubclassWindow();
      if (m_listbox.GetSafeHwnd() != NULL)
         m_listbox.UnsubclassWindow();
      CComboBox::OnDestroy();
   }
Note that for subclassing to occur, the dialog box must be painted at least once. There are cases when the dialog box doesn't paint at all (for example, closing the dialog box before it is displayed, hidden dialog boxes). This method may not be suitable when access to the subclassed windows are needed in these cases.
<MSDN>
0
 
GlennDeanCommented:
You can do this via the combo box's SetWindowText function.  

If you have a member variable say m_CB for the combobox on the dialog, go
m_CB.SetWindowText("AOL");
UpdateData(FALSE);

If you don't have a member variable, then can go
CComboBox * pCB = (CComboBox *)GetDlgItem(IDC_COMBO1);
pCB->SetWindowText("AOL");

   Glenn
0
 
KeithTeoAuthor Commented:
If I want to achieve the following effect.

1) I added "My Display Text" to the Combo List.
2) But I want only to display "My Display" on my Edit Combo.

Which event message should I intercept?
0
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!

 
GloriousRainCommented:
CBN_EDITCHANGE indicates the user has change text in the edit control
0
 
GloriousRainCommented:
or CBN_SELCHANGE indicates a new combo box list item is selected
0
 
MadYugoslavCommented:
CBN_SELENDOK
The CBN_SELENDOK notification message is sent when the user selects a list item, or selects an item and then closes the list.
0
 
KeithTeoAuthor Commented:
If I want to achieve the following effect.

1) I added "My Display Text" to the Combo List.
2) But I want only to display "My Display" on my Edit Combo.

Which event message should I intercept?
0
 
KeithTeoAuthor Commented:
If I want to achieve the following effect.

1) I added "My Display Text" to the Combo List.
2) But I want only to display "My Display" on my Edit Combo.

Which event message should I intercept?
0
 
DanRollinsCommented:
How do I get to the CEdit in a combo box?

CComboCox combo;
CEdit edit;
// Combobox creation ...
// ...
POINT tmpPoint = {1,1};
edit.SubclassWindow( combo.ChildWindowFromPoint(tmpPoint)
->GetSafeHwnd());

jahans@slb.com, mfc-l, 8/25/95

Or:

Look into the MFC sample—npp - npview.cpp! Turns out all combos create their edits with an ID of 1001 (decimal), so—if pComboBox is the pWnd object pointing to the combo—all you need is:

pComboBox->GetDlgItem(1001);

0
 
Roshan DavisCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by : GloriousRain, DanRollins (points to be split)

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer
0

Featured Post

Technology Partners: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now