5,699,431 members and growing! (15,741 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, .NET, Visual Studio, Dev

Posted: 7 Jan 2004
Updated: 7 Jan 2004
Views: 109,324
Bookmarked: 37 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
19 votes for this Article.
Popularity: 4.68 Rating: 3.66 out of 5
1 vote, 5.3%
1
1 vote, 5.3%
2
2 votes, 10.5%
3
6 votes, 31.6%
4
9 votes, 47.4%
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


~/sathishvj
Location: India India

Other popular Combo & List Boxes articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
GeneralSuggestion: Don't assign DropDownWidth to variable 'width'memberqian_xu17:54 31 Oct '07  
GeneralCouple of suggestionsmemberCleaKO11:01 30 Mar '07  
GeneralDropDown list width increment according to the value in listmembervinodgn19:28 4 Nov '06  
GeneralRe: DropDown list width increment according to the value in listmemberCleaKO10:59 30 Mar '07  
GeneralProblem with foreign language (non-English)memberKiTsuNeKo23:22 10 Nov '05  
GeneralDropDownListmemberDoomKickYourAss6:26 11 Aug '05  
GeneralVB.Net Codememberalardaen3:50 21 May '05  
Generalcustom Validator in asp .netsussDhananjaneyulud1:45 13 Dec '04  
GeneralOnly calculate when filling comboboxmemberalcoh2:36 30 Nov '04  
GeneralAllow ComboItem to be an object...memberezanker12:11 15 Jan '04  
GeneralRe: Allow ComboItem to be an object...memberoscar ho11:22 19 Jan '04  
GeneralNot working inside OnDropDownmemberSrinivaasan M22:11 13 Jan '04  
GeneralRe: Not working inside OnDropDownmemberSathishVJ7:09 14 Jan '04  
GeneralAny ChancememberGeovani2:08 13 Jan '04  
GeneralRe: Any ChancememberSathishVJ7:12 14 Jan '04  
GeneralVB.Net Codememberjasonallenbell10:16 28 Mar '07  
GeneralThis is so useful!memberilyah13:17 8 Jan '04  
GeneralI agree with youmembernetscout15:42 8 Jan '04  
GeneralRe: This is so useful!memberJcxl22: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-2008
Web10 | Advertise on the Code Project