Click here to Skip to main content
15,892,575 members
Articles / Programming Languages / C#
Article

Adjust combo box drop down list width to longest string width

Rate me:
Please Sign up or sign in to vote.
4.56/5 (40 votes)
7 Jan 2004 431.5K   66   39
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.

C#
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


Written By
France France
~/sathishvj

Comments and Discussions

 
Generalcustom Validator in asp .net Pin
Member 158550213-Dec-04 0:45
Member 158550213-Dec-04 0:45 
GeneralOnly calculate when filling combobox Pin
Ajek30-Nov-04 1:36
Ajek30-Nov-04 1:36 
GeneralAllow ComboItem to be an object... Pin
ezanker15-Jan-04 11:11
ezanker15-Jan-04 11:11 
GeneralRe: Allow ComboItem to be an object... Pin
oscar ho19-Jan-04 10:22
oscar ho19-Jan-04 10:22 
GeneralNot working inside OnDropDown Pin
Member 26089213-Jan-04 21:11
Member 26089213-Jan-04 21:11 
GeneralRe: Not working inside OnDropDown Pin
SathishVJ14-Jan-04 6:09
SathishVJ14-Jan-04 6:09 
GeneralRe: Not working inside OnDropDown Pin
Surendran126-Apr-09 21:04
Surendran126-Apr-09 21:04 
GeneralAny Chance Pin
Geovani13-Jan-04 1:08
Geovani13-Jan-04 1:08 
GeneralRe: Any Chance Pin
SathishVJ14-Jan-04 6:12
SathishVJ14-Jan-04 6:12 
GeneralVB.Net Code Pin
jasonallenbell28-Mar-07 9:16
jasonallenbell28-Mar-07 9:16 
GeneralRe: VB.Net Code Pin
Hesron M. Gozano19-Nov-11 17:35
Hesron M. Gozano19-Nov-11 17:35 
GeneralThis is so useful! Pin
ilyah8-Jan-04 12:17
ilyah8-Jan-04 12:17 
GeneralI agree with you Pin
netscout8-Jan-04 14:42
netscout8-Jan-04 14:42 
GeneralRe: This is so useful! Pin
Jcxl22-Feb-04 21:56
Jcxl22-Feb-04 21:56 

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.