Take the 2-minute tour ×

I have a C++ MFC CComboBox (VS 2010) which users can enter text and click a "save" button, which inserts their text into the dropdown list for later recall/use. When text is too long for the box I need to have a scrollbar, so I set WS_HSCROLL in the resource file and use m_Combo.SetHorizontalExtent(x), which works just fine.

The problem I have is that where there's a horizontal scroll, one line is covered by it and a vertical scroll bar appears to scroll to that one item. I have tried

m_Combo.MoveWindow(&rctDropDown) //rctDropDown was first pulled out and modified
::SetWindowPos() //called after modifying values from ::GetWindowRect()
r.OffsetRect() //where r is from m_Combo.GetDroppedControlRect(&r)

and probably more the past few days but nothing seems to override the automatic sizing of the dropdown which doesn't account for the horizontal scroll. I'm new to MFC and found these suggested online during desperate Google searches.

In short, is there a way to override the auto-height or extend it? I know how to resize it in the resource editor but I want to resize in code during runtime and everything seems to be ignored. Here are my functions from a test project that reproduced the error:

void CtestDlg::StoreClicked()
    CString l;
    m_Combo.InsertString(0, l);

void CtestDlg::UpdateList()
    // Find the longest string in the list box.
    CString     str;
    CSize       sz;
    CDC*        pDC = m_Combo.GetDC();
    CFont*      pFont = m_Combo.GetFont();

    int         x = 0;
    int         y = 0;

    // Select the listbox font, save the old font
    CFont* pOldFont = pDC->SelectObject(pFont);
    // Get the text metrics for avg char width

    for(int i = 0; i < m_Combo.GetCount(); i++)
        m_Combo.GetLBText(i, str);
        sz = pDC->GetTextExtent(str);

        // Add the avg width to prevent clipping
        sz.cx += tm.tmMaxCharWidth;

        m_Combo.SetItemHeight(i, sz.cy);

        if (sz.cx > x)
            x = sz.cx;

        y += sz.cy;
    // Select the old font back into the DC

    //manually change height here?//
share|improve this question
add comment

1 Answer

Instead of adding a horizontal scrollbar and allowing scrolling if the dropped listbox is not wide enough, you can rather just set the width of the dropped listbox accordingly.




share|improve this answer
Thank you, I know about that work around. That's my backup if I can't get this to work, but this is going on a pretty small display and a scroll is much more practical. –  mwilliams Jun 6 '12 at 20:16
add comment

Your Answer


