Change listbox width of combo boxes

.

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:
         break;
     case CTLCOLOR_LISTBOX:
         if (ListWidth > 0) {             
             // New width of a listbox is defined
             CRect rect;
             pWnd->GetWindowRect(&rect);
             if (rect.Width() != ListWidth) {
                 rect.right = rect.left + ListWidth;
                 pWnd->MoveWindow(&rect);
             }
         }
         break;
     }

     // 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.

Addenda

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



Comments

  • Width but not size

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

    Originally posted by: Eugene

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

    Reply
  • Making list box part horizontal scrollable

    Posted by Legacy on 06/29/2002 12: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.

    Reply
  • Can not draw Combo BOx

    Posted by Legacy on 06/17/2002 12: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()

    m_combo.AddString("sdad");
    m_combo.AddString("sdad");
    m_combo.AddString("sdad");
    m_combo.AddString("sdad");
    m_combo.AddString("sdad");

    m_combo.SetCurSel(1);

    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

    Reply
  • what is ListWidth

    Posted by Legacy on 03/28/2002 12: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.

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

    Posted by Legacy on 01/30/2002 12: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?

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

    Posted by Legacy on 01/14/2002 12: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.

    Reply
  • Very Nice!

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

    Originally posted by: Dimitar Andreev

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

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

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

    Originally posted by: Wayne Zhang

    There is no SetDroppedHeight() member function available

    Reply
  • size of the combo box.

    Posted by Legacy on 07/12/2001 12: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?

    Reply
  • Nice!

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

    Originally posted by: JeongHwan Cho

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

    Thanks!

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • The latest release of SugarCRM's flagship product gives users new tools to build extraordinary customer relationships. Read an in-depth analysis of SugarCRM's enhanced ability to help companies execute their customer-facing initiatives from Ovum, a leading technology research firm.

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds