Click here to Skip to main content
12,546,448 members (53,272 online)
Click here to Skip to main content
Add your own
alternative version


102 bookmarked

Setting the width of the dropdown list

, 17 May 2000 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple tutorial explaining how to set the width of a combo dropdown list so that all items are fully visible
  • Download source files - 17 Kb
  • Sample Image - ComboDropWidth.gif


    One of the more frustrating aspects of a user interface can be the situation where you are presented with a list of choices in a combobox, but are unable to view the choices because the dropdown list is too narrow.

    This article presents an extremely simple CCombobox derived class that will always present a list that is wide enough to hold the widest string.

    A new class

    To start we use the ClassWizard to create a new class CMyComboBox derived from CComboBox.

    Creating a new class

    Then use the class wizard to add an override for the reflected message CBN_DROPDOWN. A reflected message is a message that the control sends to its parent, but which the parent lets the control have a chance at processing first. The "=" before the CBN_DROPDOWN in the ClassWizard indicates it's a reflected message.

    Adding a message handler

    Call the message handler OnDropDown and add the following code

    void CMyComboBox::OnDropdown() 

    Then add the function RecalcDropWidth to your CMyComboBox. What we are doing is that each time the combo displays it's drop down, we are intercepting the message and calling a function to quickly check and the size of the control's dropdown before it gets a chance to display it.

    The code for RecalcDropWidth is as follows

    void CMyComboBox::RecalcDropWidth()
        // Reset the dropped width
        int nNumEntries = GetCount();
        int nWidth = 0;
        CString str;
        CClientDC dc(this);
        int nSave = dc.SaveDC();
        int nScrollWidth = ::GetSystemMetrics(SM_CXVSCROLL);
        for (int i = 0; i < nNumEntries; i++)
            GetLBText(i, str);
            int nLength = dc.GetTextExtent(str).cx + nScrollWidth;
            nWidth = max(nWidth, nLength);
        // Add margin space to the calculations
        nWidth += dc.GetTextExtent("0").cx;

    Thanks to Eugeny Berezkin for pointing out that the combo draws its dropdown list with a small margin.

    Essentially what we are doing is getting a device context that we can use to calculate the size of each string to be displayed in the combo. We then ensure that we are performing these calculations with the correct font, and then iterate through the list and work out the maximum width. We then add a small margin to replicate the on-screen behaviour of the drop down. Once that is done we call CComboBox::SetDroppedWidth to set the new width.

    Using the new class

    So - we now have a class that does what we want - but how do we use it?

    Simple. CMyComboBox is a drop-in replacement for CComboBox. Simply include the MyComboBox.h header file where appropriate, and change your combobox's class from CComboBox to CMyComboBox.

    Alternatively, you can use the ClassWizard to associate a variable with your combobox. Go to the Member Variables tab in ClassWizard and select the Form or Dialog class that contains your combobox control. Choose "Add Variable...", specify a name, set the Category as "Control" and then set the variable type as CMyComboBox.

    Subclassing the control

    If you used the MyComboBox files from the download files for this article and simply included them in your project without rebuilding the class file (delete the .clw file from your directory and hit Ctrl+W) then CMyComboBox may not appear in the list of available combobox classes. If this happens simply choose CComboBox and then go into your Form or Dialogs header file and manually change CComboBox to CMyComboBox.


    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


    About the Author

    Chris Maunder
    Founder CodeProject
    Canada Canada
    Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.

    His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.

    He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.

    Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

    You may also be interested in...


    Comments and Discussions

    Question5* Pin
    Jorge Silva Pereira14-Aug-15 0:21
    memberJorge Silva Pereira14-Aug-15 0:21 
    QuestionNice, thank you! Pin
    J Eakin3-Apr-14 11:46
    memberJ Eakin3-Apr-14 11:46 
    GeneralMy vote of 5 Pin
    AndrehPoffo22-Jan-13 7:00
    memberAndrehPoffo22-Jan-13 7:00 
    GeneralGave you a 5 Pin
    Hans Dietrich16-Feb-11 2:08
    mentorHans Dietrich16-Feb-11 2:08 
    GeneralRe: Gave you a 5 Pin
    Chris Maunder16-Feb-11 2:10
    adminChris Maunder16-Feb-11 2:10 
    GeneralRe: Gave you a 5 Pin
    Hans Dietrich16-Feb-11 2:15
    mentorHans Dietrich16-Feb-11 2:15 
    GeneralRe: Gave you a 5 Pin
    Chris Maunder16-Feb-11 2:34
    adminChris Maunder16-Feb-11 2:34 
    GeneralRe: Gave you a 5 Pin
    Hans Dietrich16-Feb-11 2:16
    mentorHans Dietrich16-Feb-11 2:16 
    GeneralMy vote of 5 Pin
    Hans Dietrich16-Feb-11 2:07
    mentorHans Dietrich16-Feb-11 2:07 
    GeneralGreat easy and helpful Pin
    juijuijui18-Aug-09 23:36
    memberjuijuijui18-Aug-09 23:36 
    QuestionWhy derive a new class? Pin
    alex__b14-Apr-09 19:42
    memberalex__b14-Apr-09 19:42 
    QuestionHow to increase the drop down list Height of Combobox Pin
    poda20-Feb-08 16:47
    memberpoda20-Feb-08 16:47 
    GeneralNON MFC Pin
    Ernie_D11-Apr-05 7:12
    memberErnie_D11-Apr-05 7:12 
    Generalsetting the combobox width inside a frame Pin
    deepa.n22-Feb-04 22:01
    memberdeepa.n22-Feb-04 22:01 
    GeneralThe list box of the combobox is NOT visible when on tab!! Pin
    Maria Efstathiou10-Nov-03 0:50
    sussMaria Efstathiou10-Nov-03 0:50 
    GeneralGetting rid of the whitespace that isn't necessary when the scrollbar isn't visible. Pin
    John Noël29-Aug-03 3:15
    memberJohn Noël29-Aug-03 3:15 
    Generalread a txt file witch selected by a combobox Pin
    nicolasgib1-Aug-03 17:17
    membernicolasgib1-Aug-03 17:17 
    QuestionHow to make List and Combo Box right aligned? Pin
    Dhirendra10-Jun-03 3:28
    memberDhirendra10-Jun-03 3:28 
    AnswerRe: How to make List and Combo Box right aligned? Pin
    debeyao20-Jul-03 14:57
    memberdebeyao20-Jul-03 14:57 
    GeneralRe: How to make List and Combo Box right aligned? Pin
    rahul.s@s7solutions.com28-Sep-05 23:30
    memberrahul.s@s7solutions.com28-Sep-05 23:30 
    GeneralRe: How to make List and Combo Box right aligned? Pin
    vcplusplus5-May-06 7:52
    membervcplusplus5-May-06 7:52 
    QuestionWhy using SaveDC/RestoreDC? Pin
    LarryLeonard9-May-03 4:04
    memberLarryLeonard9-May-03 4:04 
    AnswerRe: Why using SaveDC/RestoreDC? Pin
    Chris Maunder9-May-03 4:24
    adminChris Maunder9-May-03 4:24 
    Generalsetting the <i>ComboBox</i> width Pin
    henchook13-Jan-03 20:06
    memberhenchook13-Jan-03 20:06 
    GeneralRe: setting the &lt;i&gt;ComboBox&lt;/i&gt; width Pin
    mangels522-Nov-05 1:38
    membermangels522-Nov-05 1:38 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Terms of Use | Mobile
    Web02 | 2.8.161018.1 | Last Updated 18 May 2000
    Article Copyright 2000 by Chris Maunder
    Everything else Copyright © CodeProject, 1999-2016
    Layout: fixed | fluid