?
Solved

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

Posted on 2001-06-12
10
Medium Priority
?
1,228 Views
Last Modified: 2013-11-20
How to insert a text in the Edit box of a ComboBox without adding it to the list in the combobox? Anyone?
0
Comment
Question by:KeithTeo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 2

Accepted Solution

by:
GloriousRain earned 100 total points
ID: 6184212
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
 
LVL 3

Expert Comment

by:GlennDean
ID: 6184217
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
 

Author Comment

by:KeithTeo
ID: 6184816
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Expert Comment

by:GloriousRain
ID: 6184844
CBN_EDITCHANGE indicates the user has change text in the edit control
0
 
LVL 2

Expert Comment

by:GloriousRain
ID: 6184858
or CBN_SELCHANGE indicates a new combo box list item is selected
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6184991
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
 

Author Comment

by:KeithTeo
ID: 6185068
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
 

Author Comment

by:KeithTeo
ID: 6189162
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
 
LVL 49

Assisted Solution

by:DanRollins
DanRollins earned 100 total points
ID: 7049250
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
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9484653
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

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

762 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