Export (0) Print
Expand All


Call this function to set the minimum allowable width, in pixels, of the list box of a combo box.

int SetDroppedWidth(
   UINT nWidth 



The minimum allowable width of the list-box portion of the combo box, in pixels.

If successful, the new width of the list box, otherwise CB_ERR.

This function only applies to combo boxes with the CBS_DROPDOWN or CBS_DROPDOWNLIST style.

By default, the minimum allowable width of the drop-down list box is 0. When the list-box portion of the combo box is displayed, its width is the larger of the minimum allowable width or the combo box width.

// The pointer to my combo box.
extern CComboBox* pmyComboBox;

// Find the longest string in the combo box.
CString      str;
CSize      sz;
int      dx = 0;
CDC*      pDC = pmyComboBox->GetDC();
CFont*      pFont = pmyComboBox->GetFont();

// 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 < pmyComboBox->GetCount(); i++)
   pmyComboBox->GetLBText(i, str);
   sz = pDC->GetTextExtent(str);

   // Add the avg width to prevent clipping
   sz.cx += tm.tmAveCharWidth;
   if (sz.cx > dx)
      dx = sz.cx;
// Select the old font back into the DC

// Adjust the width for the vertical scroll bar and the left and right border.
dx += ::GetSystemMetrics(SM_CXVSCROLL) + 2*::GetSystemMetrics(SM_CXEDGE);

// If the width of the list box is too small, adjust it so that every
// item is completely visible.
if (pmyComboBox->GetDroppedWidth() < dx)
   ASSERT(pmyComboBox->GetDroppedWidth() == dx);

Community Additions

© 2015 Microsoft