Click here to Skip to main content
Licence 
First Posted 7 Jan 2004
Views 193,280
Bookmarked 55 times

Adjust combo box drop down list width to longest string width

By SathishVJ | 7 Jan 2004
Automatically adjust the width of the drop down list of a combo box to the width of its longest item string.
1 vote, 4.0%
1
1 vote, 4.0%
2
2 votes, 8.0%
3
6 votes, 24.0%
4
15 votes, 60.0%
5
4.54/5 - 25 votes
2 removed
μ 3.90, σa 1.89 [?]

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

About the Author

SathishVJ



France France

Member
~/sathishvj

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberHesron M. Gozano18:36 19 Nov '11  
Questioncode for webbased application PinmemberAtul Naik20:05 16 Nov '11  
Questioncode for webbased application PinmemberAtul Naik20:03 16 Nov '11  
GeneralSolution for databound combobox inside Pinmemberavalon12322:29 7 Apr '11  
GeneralMaking it work with Databound combobox Pinmemberkanreddyjp1:38 11 Oct '10  
GeneralVERY Useful PinmemberOmar Gamil9:30 16 Apr '10  
GeneralUsing extension method PinmemberC#rizje6:05 31 May '09  
QuestionCode PinmemberDanielHH8:01 28 Apr '09  
QuestionOn which event we have to bind? Pinmemberharshkapoors5:11 19 Apr '09  
GeneralPlz provide sorce code for this article.. PinmemberMember 32591773:38 10 Jan '09  
GeneralSuggestion: Don't assign DropDownWidth to variable 'width' Pinmemberqian_xu17:54 31 Oct '07  
GeneralCouple of suggestions PinmemberCleaKO11:01 30 Mar '07  
GeneralDropDown list width increment according to the value in list Pinmembervinodgn19:28 4 Nov '06  
I want to know if this method can work with a DropDownList instead of a combo box. It would be very kindful of you if you could suggest me some solution. Its really very urgent.
 


 
Vinod G Nair

GeneralRe: DropDown list width increment according to the value in list PinmemberCleaKO10:59 30 Mar '07  
GeneralProblem with foreign language (non-English) PinmemberKiTsuNeKo23:22 10 Nov '05  
GeneralDropDownList PinmemberDoomKickYourAss6:26 11 Aug '05  
GeneralVB.Net Code Pinmemberalardaen3:50 21 May '05  
Generalcustom Validator in asp .net PinsussDhananjaneyulud1:45 13 Dec '04  
GeneralOnly calculate when filling combobox Pinmemberalcoh2:36 30 Nov '04  
GeneralAllow ComboItem to be an object... Pinmemberezanker12:11 15 Jan '04  
GeneralRe: Allow ComboItem to be an object... Pinmemberoscar ho11:22 19 Jan '04  
GeneralNot working inside OnDropDown PinmemberSrinivaasan M22:11 13 Jan '04  
GeneralRe: Not working inside OnDropDown PinmemberSathishVJ7:09 14 Jan '04  
GeneralRe: Not working inside OnDropDown PinmemberSurendran122:04 26 Apr '09  
GeneralAny Chance PinmemberGeovani2:08 13 Jan '04  

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.

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