Click here to Skip to main content
13,409,836 members (38,682 online)
Click here to Skip to main content
Add your own
alternative version


50 bookmarked
Posted 25 Jul 2002

Dynamically re-creating a combo box

, 2 Aug 2002
Rate this:
Please Sign up or sign in to vote.
Function to recreate a combo box at run-time to allow new styles, preserving its data


Something which I frequently find myself needing to do is to switch a combo box to or from sorted. This is one reason (though not the main reason) I came up with my Dynamically Switchable Control. However, changing these styles after creation is something that combo-box does not support. This function presented here will recreate the combo box so that style changes take effect. The function will retain all list items, with item data, and the currently selected, or entered, text.

When the style is modified on a combobox to include or remove the CBS_SORT for example, it can be seen (by using GetStyle(), or using a tool such as Spy++) that the control does in fact have the new style, and yet the control's behaviour has not changed. The function here simply gets the current info from the control, i.e. styles, font etc, and recreates the control using these values.

How to use

I originally had a CComboBox-derived class which one member function to recreate the controls, but decided that this was more trouble than it was worth, as most of my combos derived from other things. Therefore I have presented this as a single function, which should be included in a global library file, so that you can invoke it on any combobox at any time.

In order to change the style of a combo, you should perform the usual method of setting the new style e.g.

combo.ModifyStyle(0, CBS_SORT);

and then after that call you should call:


The function takes an optional void pointer, lpParam, which is passed to CreateEx on recreating the control. If you have special requirements when creating your controls, and you normally pass in some data for the creation parameters, then you should pass the same info in here. Most developers can simply ignore this parameter.

The Function

The function to recreate the combo is below:

// recreate the combo box by copying styles etc, and list items
// and applying them to a newly created control
BOOL RecreateComboBox(CComboBox* pCombo, LPVOID lpParam/*=0*/)
	if (pCombo == NULL)
		return FALSE;
	if (pCombo->GetSafeHwnd() == NULL)
		return FALSE;

	CWnd* pParent = pCombo->GetParent();
	if (pParent == NULL)
		return FALSE;

	// get current attributes
	DWORD dwStyle = pCombo->GetStyle();
	DWORD dwStyleEx = pCombo->GetExStyle();
	CRect rc;
	pParent->ScreenToClient(&rc);	// map to client co-ords
	UINT nID = pCombo->GetDlgCtrlID();
	CFont* pFont = pCombo->GetFont();
	CWnd* pWndAfter = pCombo->GetNextWindow(GW_HWNDPREV);

	// get the currently selected text (and whether it is a valid list selection)
	CString sCurText;
	int nCurSel = pCombo->GetCurSel();
	BOOL bItemSelValid = nCurSel != -1;
	if (bItemSelValid)
		pCombo->GetLBText(nCurSel, sCurText);

	// copy the combo box items into a temp combobox (not sorted)
	// along with each item's data
	CComboBox comboNew;
	if (! comboNew.CreateEx(dwStyleEx, _T("COMBOBOX"), _T(""), 
                                 dwStyle, rc, pParent, nID, lpParam))
	  return FALSE;
	int nNumItems = pCombo->GetCount();
	for (int n = 0; n < nNumItems; n++)
		CString sText;
		pCombo->GetLBText(n, sText);
		int nNewIndex = comboNew.AddString(sText);
		comboNew.SetItemData(nNewIndex, pCombo->GetItemData(n));
	// re-set selected text
	if (bItemSelValid)
		comboNew.SetCurSel(comboNew.FindStringExact(-1, sCurText));

	// destroy the existing window, then attach the new one
	HWND hwnd = comboNew.Detach();

	// position correctly in z-order
	pCombo->SetWindowPos(pWndAfter == NULL ? 
                                &CWnd::wndBottom : 
                               pWndAfter, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

	return TRUE;

See Also


  • Version 1 - 26 Jul 2002 - First version
  • Version 2 - 30 Jul 2002 - modified to include changes suggested by Jean-Michel LE FOL and Davide Zaccanti


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Paul Vickery
Software Developer (Senior)
United Kingdom United Kingdom
Originally from an electronics background, I moved into software in 1996, partly as a result of being made redundant, and partly because I was very much enjoying the small amount of coding (in-at-the-deep-end-C) that I had been doing!

I swiftly moved from C to C++, and learned MFC, and then went on to real-time C on Unix. After this I moved to the company for which I currently work, which specialises in Configuration Management software, and currently program mainly in C/C++, for Windows. I have been gradually moving their legacy C code over to use C++ (with STL, MFC, ATL, and WTL). I have pulled in other technologies (Java, C#, VB, COM, SOAP) where appropriate, especially when integrating with third-party products.

In addition to that, I have overseen the technical side of the company website (ASP, VBScript, JavaScript, HTML, CSS), and have also worked closely with colleagues working on other products (Web-based, C#, ASP.NET, SQL, etc).

For developing, I mainly use Visual Studio 2010, along with an in-house-designed editor based on Andrei Stcherbatchenko's syntax parsing classes, and various (mostly freeware) tools. For website design, I use Dreaweaver CS3.

When not developing software, I enjoy listening to and playing music, playing electric and acoustic guitars and mandolin.

You may also be interested in...

Comments and Discussions

GeneralThank you very mach! I have found it for long time.^_^ Pin
libbyliugang13-May-08 8:15
memberlibbyliugang13-May-08 8:15 
Generalthanx! Pin
tortexy16-Jan-08 8:13
membertortexy16-Jan-08 8:13 
GeneralThis is example Pin
VC Spark20-Jun-07 0:14
memberVC Spark20-Jun-07 0:14 
GeneralRe: This is example Pin
VC Spark21-Jun-07 2:53
memberVC Spark21-Jun-07 2:53 
GeneralRe: This is example Pin
Mikie2-Sep-09 5:25
memberMikie2-Sep-09 5:25 
Generalthanks Pin
bizulk1-Sep-05 9:08
memberbizulk1-Sep-05 9:08 
GeneralControl Disappear Pin
JACCY24-Mar-05 5:11
sussJACCY24-Mar-05 5:11 
GeneralRe: Control Disappear Pin
Paul S. Vickery26-Apr-05 4:49
memberPaul S. Vickery26-Apr-05 4:49 
GeneralRe: Control Disappear Pin
Marco F6-Oct-05 23:48
memberMarco F6-Oct-05 23:48 
AnswerRe: Control Disappear Pin
Marco F6-Oct-05 23:55
memberMarco F6-Oct-05 23:55 
GeneralSuggestion .... Pin
CheGueVerra5-Nov-04 11:49
memberCheGueVerra5-Nov-04 11:49 
GeneralSlightly off-topic question: combo drop-down arrow Pin
David Pritchard22-Sep-04 7:48
memberDavid Pritchard22-Sep-04 7:48 
GeneralRe: Slightly off-topic question: combo drop-down arrow Pin
Rich Frank6-Nov-04 14:32
memberRich Frank6-Nov-04 14:32 
GeneralRe: Slightly off-topic question: combo drop-down arrow Pin
David Pritchard7-Nov-04 7:01
memberDavid Pritchard7-Nov-04 7:01 
GeneralRe: Slightly off-topic question: combo drop-down arrow Pin
David Pritchard7-Nov-04 17:40
memberDavid Pritchard7-Nov-04 17:40 
GeneralRe: Slightly off-topic question: combo drop-down arrow Pin
Rich Frank8-Nov-04 0:57
memberRich Frank8-Nov-04 0:57 
GeneralRe: Slightly off-topic question: combo drop-down arrow Pin
David Pritchard8-Nov-04 1:31
memberDavid Pritchard8-Nov-04 1:31 
QuestionIs it possible with ATL ? Pin
sanpro25-Dec-03 4:51
membersanpro25-Dec-03 4:51 
GeneralNo more vert. scrollbar Pin
Simon66614-Nov-02 1:09
sussSimon66614-Nov-02 1:09 
GeneralRe: No more vert. scrollbar Pin
Simon66614-Nov-02 1:16
sussSimon66614-Nov-02 1:16 
GeneralZOrder Pin
Davide Zaccanti26-Jul-02 22:55
memberDavide Zaccanti26-Jul-02 22:55 
GeneralRe: ZOrder Pin
Paul S. Vickery30-Jul-02 4:06
memberPaul S. Vickery30-Jul-02 4:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180221.1 | Last Updated 3 Aug 2002
Article Copyright 2002 by Paul Vickery
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid