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.
and then after that call you should call:
The function takes an optional void pointer,
lpParam, which is passed to
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 to recreate the combo is below:
BOOL RecreateComboBox(CComboBox* pCombo, LPVOID lpParam)
if (pCombo == NULL)
if (pCombo->GetSafeHwnd() == NULL)
CWnd* pParent = pCombo->GetParent();
if (pParent == NULL)
DWORD dwStyle = pCombo->GetStyle();
DWORD dwStyleEx = pCombo->GetExStyle();
pParent->ScreenToClient(&rc); UINT nID = pCombo->GetDlgCtrlID();
CFont* pFont = pCombo->GetFont();
CWnd* pWndAfter = pCombo->GetNextWindow(GW_HWNDPREV);
int nCurSel = pCombo->GetCurSel();
BOOL bItemSelValid = nCurSel != -1;
if (! comboNew.CreateEx(dwStyleEx, _T("COMBOBOX"), _T(""),
dwStyle, rc, pParent, nID, lpParam))
int nNumItems = pCombo->GetCount();
for (int n = 0; n < nNumItems; n++)
int nNewIndex = comboNew.AddString(sText);
HWND hwnd = comboNew.Detach();
pCombo->SetWindowPos(pWndAfter == NULL ?
pWndAfter, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
- 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
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.
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.