Change listbox width of combo boxes


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame


When using combo boxes of type dropdown and droplist, the with of the listbox is the same as the width of the combobox. In some cases, it may be useful to have the listbox part wider then the combobox.

To do this, add a handler to WM_CTLCOLOR message to your dialog or to a class derived from CCombobox and use the following code:

HBRUSH TVisualCombo::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
     HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
     switch (nCtlColor) {
         case CTLCOLOR_EDIT:
         if (ListWidth > 0) {             
             // New width of a listbox is defined
             CRect rect;
             if (rect.Width() != ListWidth) {
                 rect.right = rect.left + ListWidth;

     // TODO: Return a different brush if the default is not desired
     return hbr;

Using this technique, it is also possible to update a listbox width so that listbox always shows the complete text of all the listbox items. In order to do this, you have to scan the listbox items and calculate the text extent (using pDC) and then update the listbox width so that it is longer then the longest listbox item.


My original article regarding the changing of the listbox width of comboboxes used a WM_CTLCOLOR message. However, Katy Mulvey was kind enough (thanks Katy) to point out that the width of the listbox can be modified with a standard MFC CComboBox class member function.

CComboBox::SetDroppedWidth(int width);

You specify the minimal width with this function. The actual width is the largest value of the following values:

  • The minimum width (set with previous function).
  • The combobox width.

Last updated: October 2, 1998


  • Width but not size

    Posted by Legacy on 10/21/2002 07:00am

    Originally posted by: Eugene

    It's known problem.
    But Size of drop-down list!!!! It's lost

  • Making list box part horizontal scrollable

    Posted by Legacy on 06/29/2002 07:00am

    Originally posted by: Gemiur

    I found an example from MSDN on how to use SetHorizontalExtent function to make list box part horizontal scrollable. But it didn't work. Anyone knows how to implement it. Appreciate any response.

  • Can not draw Combo BOx

    Posted by Legacy on 06/17/2002 07:00am

    Originally posted by: AAG

    I draw combobox on dialog. but when i execute my program & when i click on vertical scroll the combobox did not appear as drop down i set property visible=true, i make sort=true,vertical scroll = true,type= drop down , ownerdraw=no,
    i insert following code in InitDialog()



    but my program doesn't work properly, when i click on vertical scroll of combo it does not dropsdown, it it requires additonal code then please give me the complete procedure only to draw combobox on dialog

    Thank u

  • what is ListWidth

    Posted by Legacy on 03/28/2002 08:00am

    Originally posted by: Arun

    since i am new to VC++ struggling to build one ocx, in which your tips also useful to me to expand the width of the listbox.... can u just guide me what is that listwidth and from where to get the listwidth... etc.

  • I did the one using pDC to determine the maximum width but...

    Posted by Legacy on 01/30/2002 08:00am

    Originally posted by: Michael Lance

    ...everywhere I do that, the width is always much wider than needed. I have heard a rumor that the DC used to finally draw the list box contents has a different font. Am I missing something between m_hDC and m_hAttribDC?

  • I was having troubles invalidating the listbox until I saw this.

    Posted by Legacy on 01/14/2002 08:00am

    Originally posted by: Will Pittenger

    My combobox contains items whose names constantly change. (Every second, a time in name is updated.) The only technique available when I was using a plain combobox was to empty it and refill. That prevented the user from tracking the item they wanted because it ceased to exist in the combobox every second. This became really hard if the user had to scroll the list to get their item. We also need to change the sort order to match a numerical ID (Item 2 had to be before Item 10).

    I tried to solve the problem by making the combobox owner drawn as a class derived from CComboBox. I overrode CompareItem to solve the sorting problem. I ensured that the name displayed was not looked up until DrawItem was painting that name. Next, I added a function that was called by the parent after the item names had changed which invalidated the combobox.

    Unfortunately, that didn't affect the list box portion of the combobox (DROPLIST style). There seemed to be no way to retrieve a handle to the listbox under Windows 95 or Windows NT SP5 or older. I was ready to abandon the combobox altogether when I saw this article.

    The article did not match what I needed, but it did help. The author used the combobox's OnCtlColor to access listbox's HWND. So, I memorized that and when the item names changed, I was able to invalidate the listbox. It is now working.

  • Very Nice!

    Posted by Legacy on 08/28/2001 07:00am

    Originally posted by: Dimitar Andreev

    It's a nice method to get combobox's list window handle!

  • How can we modify the height of the dropdown list?

    Posted by Legacy on 08/01/2001 07:00am

    Originally posted by: Wayne Zhang

    There is no SetDroppedHeight() member function available

  • size of the combo box.

    Posted by Legacy on 07/12/2001 07:00am

    Originally posted by: Sunil Agrawal

    When we add a combo box resource in the dialog, the width remains constant. How can we increase the width of the combo box?

  • Nice!

    Posted by Legacy on 05/14/2001 07:00am

    Originally posted by: JeongHwan Cho

    It's a nice method to get combobox's list window handle!


  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date