Click here to Skip to main content
12,627,377 members (33,270 online)
Rate this:
Please Sign up or sign in to vote.
See more: MFC
I have been trying to use a drop list combo box in a toolbar using the MFC feature pack. I can get the combo box to appear, but it is always disabled and never has any entries in it.

How I prepare it:

// the font used by the controls shown
CFont stockFont;
// determine the size each control will need to be
int widthCombo = 0;
    CString itemText;
    // create a DC to measure the text size to use
    CWindowDC dc(NULL);
    // now measure how big the combo box needs to be
    widthCombo = dc.GetTextExtent(itemText).cx;
    // include borders for the combobox control
    widthCombo += GetSystemMetrics(SM_CXBORDER) * 2; // left and right borders
// its ok to use the stack as the toolbar takes a copy of the CMFCToolBarComboBoxButton object
CMFCToolBarComboBoxButton comboBox(ID_REPORT_SELECT_COMBO, -1, CBS_DROPDOWNLIST, widthCombo);
m_wndToolBar.ReplaceButton(ID_REPORT_SELECT_COMBO, comboBox, TRUE);
So, I measure some default text to work out how big the combo box should be then I do the 2 lines at the end to replace the button with the combobox.

All works correctly up till this point. I then try and populate the combobox:

// put the list of available report formats into the control
// try and preserve any existing selection at the time of repopulating
// as the previously selected report style may have been renamed or deleted
int iIndex = f_FindToolbarItemIndex(m_wndToolBar, ID_REPORT_SELECT_COMBO);
if (iIndex >= 0)
    CMFCToolBarButton * pButton = m_wndToolBar.GetButton(iIndex);
    CMFCToolBarComboBoxButton * pComboButton = dynamic_cast<CMFCToolBarComboBoxButton*>(pButton);
    int sel = pComboButton->GetCurSel();
    if (sel != CB_ERR)
        sel = pComboButton->GetItemData(sel); // index of selected report configuration
    // now add the report styles available
    DCBL::ReportLayoutRepository * repository = DCBL::ReportLayoutRepository::GetInstance();
    ASSERT(repository != NULL);
    size_t count = repository->NumReportLayouts();
    sel = min(sel, count - 1);  // limit selection to valid range
    sel = max(0, sel);          // possibly no previous selection

    int selectIndex = 0;
    for (size_t index = 0 ; index < count ; ++index)
        CString name = repository->Layout(index).Name().c_str();
        int itemIndex = pComboButton->AddItem(name, index); // sort style
        if (index == sel)
            selectIndex = index;
    // check that the items are being added
    int x = pComboButton->GetCount();

At the end, the value of x is 17 in my text case, so 17 items have been added to the combobox thats in the toolbar. Yet it always shows as disabled with no items.

This stuff should be easy in the feature pack, but I have been hitting my head against a wall on this one. Any suggestions or possible missed steps?
Posted 26-Oct-12 8:07am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Ok, it was down to initialisation order. I was adding the content to the control, but I was doing this in the OnCreate of the mainframe class. This combobox content would then be thrown away when the application restored its state from any data serialzed to the the registry.

I only needed to add the combo content after the application had already performed that step.
Eugen Podsypalnikov 26-Oct-12 13:51pm
That's right, see also the reset-toolbar usage here: :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

As a quick note, if i add an UPDATE_COMMAND_UI handler for the control in the CMainFrame class I can enable the control, yet it still has no content even though I have definiately populated it.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Please try to process:
m_wndToolBar.ReplaceButton(ID_REPORT_SELECT_COMBO, comboBox, TRUE);
then provide a command reaction for your parent frame:
void CYourFrame::OnClickReportCB()
  // nothing yet, just enable the button...
It should make the box enabled :)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 26 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100