Click here to Skip to main content
Click here to Skip to main content

Adjust combo box drop down list width to longest string width

, 7 Jan 2004
Rate this:
Please Sign up or sign in to vote.
Automatically adjust the width of the drop down list of a combo box to the width of its longest item string.

Sample Image - ComboBoxAutoWidth.jpg


This small snippet of code will show you how to automatically adjust the size of the drop down list of a combo box to fit the size of the longest string in its items.


Step 1: Add an event handler for the DropDown event of the combo box. Call it AdjustWidthComboBox_DropDown for the sake of the following code.

Step 2: Add the following event handler code.

private void AdjustWidthComboBox_DropDown(object sender, System.EventArgs e)
    ComboBox senderComboBox = (ComboBox)sender;
    int width = senderComboBox.DropDownWidth;
    Graphics g = senderComboBox.CreateGraphics();
    Font font = senderComboBox.Font;
    int vertScrollBarWidth = 

    int newWidth;
    foreach (string s in ((ComboBox)sender).Items)
        newWidth = (int) g.MeasureString(s, font).Width 
            + vertScrollBarWidth;
        if (width < newWidth )
            width = newWidth;
    senderComboBox.DropDownWidth = width;


The code assumes that the handler is only for a ComboBox and does a cast without checking the type of the sender.

It then gets the Graphics and Font objects for the combo box which will help us to measure the size of the string in the list.

senderComboBox.Items.Count>senderComboBox.MaxDropDownItems checks whether a scrollbar will be displayed. If it is going to be displayed, then we get its width to adjust the size of the drop down list accordingly.

We then scroll through the list of items checking the size of each item and finally setting the width of the drop down list to the width of the largest item including scroll bar width.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author


France France

Comments and Discussions

QuestionInvalidcastException PinmemberMember 1084897527-May-14 23:45 
QuestionBest Solutions Pinprofessionalnkarankal22-Nov-13 20:53 
GeneralMy vote of 5 PinmemberMahesh Sanika21-Aug-13 1:45 
GeneralThanks Pinmemberor19717-Jun-12 6:45 
GeneralMy vote of 5 PinmemberHesron M. Gozano19-Nov-11 18:36 
Questioncode for webbased application PinmemberAtul Naik16-Nov-11 20:05 
Questioncode for webbased application PinmemberAtul Naik16-Nov-11 20:03 
GeneralSolution for databound combobox inside Pinmemberavalon1237-Apr-11 22:29 
GeneralMaking it work with Databound combobox Pinmemberkanreddyjp11-Oct-10 1:38 
GeneralVERY Useful PinmemberOmar Gamil16-Apr-10 9:30 
GeneralUsing extension method PinmemberC#rizje31-May-09 6:05 
QuestionCode PinmemberDanielHH28-Apr-09 8:01 
QuestionOn which event we have to bind? Pinmemberharshkapoors19-Apr-09 5:11 
GeneralPlz provide sorce code for this article.. PinmemberMember 325917710-Jan-09 3:38 
GeneralSuggestion: Don't assign DropDownWidth to variable 'width' Pinmemberqian_xu31-Oct-07 17:54 
GeneralCouple of suggestions PinmemberCleaKO30-Mar-07 11:01 
GeneralDropDown list width increment according to the value in list Pinmembervinodgn4-Nov-06 19:28 
GeneralRe: DropDown list width increment according to the value in list PinmemberCleaKO30-Mar-07 10:59 
GeneralProblem with foreign language (non-English) PinmemberKiTsuNeKo10-Nov-05 23:22 
GeneralDropDownList PinmemberDoomKickYourAss11-Aug-05 6:26 
GeneralVB.Net Code Pinmemberalardaen21-May-05 3:50 
Generalcustom Validator in asp .net PinsussDhananjaneyulud13-Dec-04 1:45 
GeneralOnly calculate when filling combobox Pinmemberalcoh30-Nov-04 2:36 
GeneralAllow ComboItem to be an object... Pinmemberezanker15-Jan-04 12:11 
GeneralRe: Allow ComboItem to be an object... Pinmemberoscar ho19-Jan-04 11:22 
GeneralNot working inside OnDropDown PinmemberSrinivaasan M13-Jan-04 22:11 
GeneralRe: Not working inside OnDropDown PinmemberSathishVJ14-Jan-04 7:09 
GeneralRe: Not working inside OnDropDown PinmemberSurendran126-Apr-09 22:04 
GeneralAny Chance PinmemberGeovani13-Jan-04 2:08 
GeneralRe: Any Chance PinmemberSathishVJ14-Jan-04 7:12 
GeneralVB.Net Code Pinmemberjasonallenbell28-Mar-07 10:16 
GeneralRe: VB.Net Code PinmemberHesron M. Gozano19-Nov-11 18:35 
GeneralThis is so useful! Pinmemberilyah8-Jan-04 13:17 
GeneralI agree with you Pinmembernetscout8-Jan-04 15:42 
GeneralRe: This is so useful! PinmemberJcxl22-Feb-04 22:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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.150129.1 | Last Updated 8 Jan 2004
Article Copyright 2004 by SathishVJ
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid