Skip to main content
Email Password   helpLost your password?

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.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralUsing extension method Pin
C#rizje
6:05 31 May '09  
QuestionCode Pin
DanielHH
8:01 28 Apr '09  
GeneralOn which event we have to bind? Pin
harshkapoors
5:11 19 Apr '09  
GeneralPlz provide sorce code for this article.. Pin
Member 3259177
3:38 10 Jan '09  
GeneralSuggestion: Don't assign DropDownWidth to variable 'width' Pin
qian_xu
17:54 31 Oct '07  
GeneralCouple of suggestions Pin
CleaKO
11:01 30 Mar '07  
GeneralDropDown list width increment according to the value in list Pin
vinodgn
19:28 4 Nov '06  
GeneralRe: DropDown list width increment according to the value in list Pin
CleaKO
10:59 30 Mar '07  
GeneralProblem with foreign language (non-English) Pin
KiTsuNeKo
23:22 10 Nov '05  
GeneralDropDownList Pin
DoomKickYourAss
6:26 11 Aug '05  
GeneralVB.Net Code Pin
alardaen
3:50 21 May '05  
Generalcustom Validator in asp .net Pin
Dhananjaneyulud
1:45 13 Dec '04  
GeneralOnly calculate when filling combobox Pin
alcoh
2:36 30 Nov '04  
GeneralAllow ComboItem to be an object... Pin
ezanker
12:11 15 Jan '04  
GeneralRe: Allow ComboItem to be an object... Pin
oscar ho
11:22 19 Jan '04  
GeneralNot working inside OnDropDown Pin
Srinivaasan M
22:11 13 Jan '04  
GeneralRe: Not working inside OnDropDown Pin
SathishVJ
7:09 14 Jan '04  
GeneralRe: Not working inside OnDropDown Pin
Surendran1
22:04 26 Apr '09  
GeneralAny Chance Pin
Geovani
2:08 13 Jan '04  
GeneralRe: Any Chance Pin
SathishVJ
7:12 14 Jan '04  
GeneralVB.Net Code Pin
jasonallenbell
10:16 28 Mar '07  
GeneralThis is so useful! Pin
ilyah
13:17 8 Jan '04  
GeneralI agree with you Pin
netscout
15:42 8 Jan '04  
GeneralRe: This is so useful! Pin
Jcxl
22:56 22 Feb '04  


Last Updated 7 Jan 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009