Click here to Skip to main content
6,291,722 members and growing! (12,805 online)
Email Password   helpLost your password?
Desktop Development » Combo & List Boxes » General     Intermediate

Adjust combo box drop down list width to longest string width

By SathishVJ

Automatically adjust the width of the drop down list of a combo box to the width of its longest item string.
C#, Windows, .NET 1.1, Visual Studio, Dev
Posted:7 Jan 2004
Views:126,941
Bookmarked:42 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
20 votes for this article.
Popularity: 4.86 Rating: 3.74 out of 5
1 vote, 5.0%
1
1 vote, 5.0%
2
2 votes, 10.0%
3
6 votes, 30.0%
4
10 votes, 50.0%
5

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


Member
~/sathishvj
Location: India India

Other popular Combo & List Boxes articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 24 of 24 (Total in Forum: 24) (Refresh)FirstPrevNext
GeneralUsing extension method PinmemberC#rizje6:05 31 May '09  
QuestionCode PinmemberDanielHH8:01 28 Apr '09  
GeneralOn 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  
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  
GeneralRe: Any Chance PinmemberSathishVJ7:12 14 Jan '04  
GeneralVB.Net Code Pinmemberjasonallenbell10:16 28 Mar '07  
GeneralThis is so useful! Pinmemberilyah13:17 8 Jan '04  
GeneralI agree with you Pinmembernetscout15:42 8 Jan '04  
GeneralRe: This is so useful! PinmemberJcxl22:56 22 Feb '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 Jan 2004
Editor: Smitha Vijayan
Copyright 2004 by SathishVJ
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project