Solved

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

Posted on 2001-06-12
10
1,071 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
10 Comments
 
LVL 2

Accepted Solution

by:
GloriousRain earned 25 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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 2

Expert Comment

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

Expert Comment

by:GloriousRain
Comment Utility
or CBN_SELCHANGE indicates a new combo box list item is selected
0
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

 
LVL 2

Expert Comment

by:MadYugoslav
Comment Utility
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
Comment Utility
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
Comment Utility
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 25 total points
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
matchUp  challenge 6 48
haveThree challenge 22 100
noX challenge 17 75
Change to event 1 71
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

13 Experts available now in Live!

Get 1:1 Help Now