Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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;
stockFont.Attach(GetStockObject(DEFAULT_GUI_FONT));

// 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);
    dc.SaveDC();
    dc.SelectObject(&stockFont);

    // now measure how big the combo box needs to be
    itemText.LoadString(IDS_CONTROL_SIZING_TEXT);
    widthCombo = dc.GetTextExtent(itemText).cx;
    dc.RestoreDC(-1);
    // 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);
stockFont.Detach();

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
    }
    pComboButton->RemoveAllItems();
    // 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;
        }
    }
    pComboButton->SelectItem(selectIndex);
    // 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

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.
 
Share this answer
 
Comments
Eugen Podsypalnikov 26-Oct-12 13:51pm    
That's right, see also the reset-toolbar usage here:
http://www.wretch.cc/blog/kuolun1003/13910292 :)
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.
 
Share this answer
 
Please try to process:
C++
comboBox.EnableWindow(true);

before:
C++
m_wndToolBar.ReplaceButton(ID_REPORT_SELECT_COMBO, comboBox, TRUE);

then provide a command reaction for your parent frame:
C++
BEGIN_MESSAGE_MAP(CYourFrame, CFrameWndEx)
  //..
  ON_COMMAND(ID_CBBOX_CONN_PORT, &CYourFrame::OnClickReportCB)
  //..
END_MESSAGE_MAP()

void CYourFrame::OnClickReportCB()
{
  // nothing yet, just enable the button...
}

It should make the box enabled :)
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900