May 282009

CComboBox has a function called SetHorizontalExtent which doesn’t work. SDK equivalent is CB_SETHORIZONTALEXTENT which also doesn’t work. The reason for this bug is pretty lame, because WS_HSCROLL style for combo box is not set, which in turn the VS dialog editor does not provide :(. So a workaround is to open .rc file in a text editor and add WS_HSCROLL style manually for this combo.

This is how a combo box will look with a horizontal scrollbar…

Horizontal scrollbar in a combo box

Horizontal scrollbar in a combo box

Here is a sample code from MSDN which is adapted a bit for this purpose…

void CDialoTestDlg::AddHScroll( CComboBox& Cmb )
  // Find the longest string in the combo box.
  CString str;
  CSize sz;
  int dx = 0;

  TEXTMETRIC tm = { 0 };
  CDC* pDC = Cmb.GetDC();
  CFont* pFont = Cmb.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 < Cmb.GetCount(); i++)
    Cmb.GetLBText(i, str);
    sz = pDC->GetTextExtent(str);

    // Add the avg width to prevent clipping += tm.tmAveCharWidth;

    if ( > dx)
      dx =;

  // Select the old font back into the DC

  // Set the horizontal extent so every character of all strings can
  // be scrolled to.
}// End AddHScroll

Setting horizontal extent is useful to prevent the combo’s drop down width from extending beyond screen limits. Note that there is another function called SetDroppedWidth which sets the drop down’s width but with no scrolling.

  4 Responses to “Add horizontal scrollbar to a combo box”

  1. Hi Nibu,

    I just found your article, I had the same problem some months ago and solved it in a similar way. But then I found another problem that’s driving me crazy, and I believe you probably also found. When you have a combobox with an horizontal scrollbar and a few items (let’s say 2), less than the maximum height for the dropdown list, you’ll notice that the vertical scrollbar appears always. That’s because when the horizontal scrollbar was added the dropdown list wasn’t resized, so its height is not enough to display all of the items. So let’s say you have 2 items, only one will be displayed and there’ll be a tiny vertical scrollbar on the right.
    I tried to resize the dropdown list window, the combobox window… to no avail. Now I was going to catch WM_SIZE messages, but if you have any hints…


  2. Hi Nibu…

    Was hoping I’d see you at this year’s summit … maybe the next one… assuming I get re-awarded 🙂

    Great post. It’s amazing the horizontal extents still don’t work right. I have a post on that in my legacy menu section:

    I originally wrote the article in 1999 and haven’t recompiled since VS2005, but the problem I addressed was still there then.


    • Hi Dave,

      This years summit was a big miss for me, poor planning from side. Hope if re-awarded I’ll there next time and see you then.

      Well I too didn’t check this with VS2005/2008. I still use VC6 for compiling most of my apps since my company uses VC6. We’re waiting for MS to deliver us something like VC6. 🙂

      I’ll have to test this behavior with latest compiler and SDK to see if this problem still exists. This post was in response to a user’s query in a forum. So I guess this problem is still there.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.