Click here to Skip to main content
13,900,891 members
Click here to Skip to main content
Add your own
alternative version

Stats

380.4K views
66 bookmarked
Posted 7 Jan 2004
Licenced

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

Introduction

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.

Code

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 = 
        (senderComboBox.Items.Count>senderComboBox.MaxDropDownItems)
        ?SystemInformation.VerticalScrollBarWidth:0;

    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;
}

Explanation

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.

License

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

Share

About the Author

SathishVJ
France France
~/sathishvj

You may also be interested in...

Comments and Discussions

 
QuestionImplementation sample for C++/CLI Pin
Damian J. Suess13-Nov-18 10:18
memberDamian J. Suess13-Nov-18 10:18 
Generalperhaps incomplete code? Pin
black__cat19-Mar-18 14:39
memberblack__cat19-Mar-18 14:39 
QuestionThank you Pin
aazam rafiee zade25-May-16 4:12
memberaazam rafiee zade25-May-16 4:12 
QuestionInvalidcastException Pin
Member 1084897527-May-14 22:45
memberMember 1084897527-May-14 22:45 
QuestionBest Solutions Pin
nkarankal22-Nov-13 19:53
professionalnkarankal22-Nov-13 19:53 
GeneralMy vote of 5 Pin
Mahesh Sanika21-Aug-13 0:45
memberMahesh Sanika21-Aug-13 0:45 
GeneralThanks Pin
or19717-Jun-12 5:45
memberor19717-Jun-12 5:45 
GeneralMy vote of 5 Pin
Hesron M. Gozano19-Nov-11 17:36
memberHesron M. Gozano19-Nov-11 17:36 
Questioncode for webbased application Pin
Atul Naik16-Nov-11 19:05
memberAtul Naik16-Nov-11 19:05 
Questioncode for webbased application Pin
Atul Naik16-Nov-11 19:03
memberAtul Naik16-Nov-11 19:03 
GeneralSolution for databound combobox inside Pin
avalon1237-Apr-11 21:29
memberavalon1237-Apr-11 21:29 
GeneralMaking it work with Databound combobox Pin
kanreddyjp11-Oct-10 0:38
memberkanreddyjp11-Oct-10 0:38 
GeneralVERY Useful Pin
Omar Gameel Salem16-Apr-10 8:30
professionalOmar Gameel Salem16-Apr-10 8:30 
GeneralUsing extension method Pin
C#rizje31-May-09 5:05
memberC#rizje31-May-09 5:05 
QuestionCode Pin
DanielHH28-Apr-09 7:01
memberDanielHH28-Apr-09 7:01 
QuestionOn which event we have to bind? Pin
harshkapoors19-Apr-09 4:11
memberharshkapoors19-Apr-09 4:11 
GeneralPlz provide sorce code for this article.. Pin
Member 325917710-Jan-09 2:38
memberMember 325917710-Jan-09 2:38 
GeneralSuggestion: Don't assign DropDownWidth to variable 'width' Pin
qian_xu31-Oct-07 16:54
memberqian_xu31-Oct-07 16:54 
GeneralCouple of suggestions Pin
Marcus J. Smith30-Mar-07 10:01
professionalMarcus J. Smith30-Mar-07 10:01 
GeneralDropDown list width increment according to the value in list Pin
vinodgn4-Nov-06 18:28
membervinodgn4-Nov-06 18:28 
GeneralRe: DropDown list width increment according to the value in list Pin
Marcus J. Smith30-Mar-07 9:59
professionalMarcus J. Smith30-Mar-07 9:59 
GeneralProblem with foreign language (non-English) Pin
AesopTurtle10-Nov-05 22:22
memberAesopTurtle10-Nov-05 22:22 
GeneralDropDownList Pin
DoomKickYourAss11-Aug-05 5:26
memberDoomKickYourAss11-Aug-05 5:26 
GeneralVB.Net Code Pin
alardaen21-May-05 2:50
memberalardaen21-May-05 2:50 
Generalcustom Validator in asp .net Pin
Member 158550213-Dec-04 0:45
memberMember 158550213-Dec-04 0:45 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190306.1 | Last Updated 8 Jan 2004
Article Copyright 2004 by SathishVJ
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid