Click here to Skip to main content
Rate this: bad
good
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;
    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 26-Oct-12 7:07am
Rate this: bad
good
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.
  Permalink  
Comments
Eugen Podsypalnikov at 26-Oct-12 13:51pm
   
That's right, see also the reset-toolbar usage here:
http://www.wretch.cc/blog/kuolun1003/13910292 :)
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Please try to process:
comboBox.EnableWindow(true);
before:
m_wndToolBar.ReplaceButton(ID_REPORT_SELECT_COMBO, comboBox, TRUE);
then provide a command reaction for your parent frame:
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 Smile | :)
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 26 Oct 2012
Copyright © CodeProject, 1999-2014
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