Combo Box with text longer than the control

Posted on 2005-04-05
Last Modified: 2010-04-16
In VS.NET, when a combo box has a text string that is longer than the control, the drop downs size will expand to accomodate.  This does not happen when using the traditional drop down controls in my program.  Is there someway to get this functionality?
Question by:jjacksn
    LVL 11

    Accepted Solution

    I'm fairly new to C#, but maybe you can resize the drop down width using the dropdownwidth property. The only thing that I'm missing is how to accurately convert string length to pixel length since some letter take up less space than others (i vs. x).

    Hope this will maybe give you some ideas:

    string str = "";
    for (int i=0; i<this.comboBox1.Items.Count; i++)
          if (str.Length < this.comboBox1.Items[i].ToString().Length)
                str = this.comboBox1.Items[i].ToString();
          this.comboBox1.DropDownWidth = Convert.ToInt32(str.Length*3.5);
    LVL 6

    Assisted Solution

    You can used this code:


        System.Drawing.Graphics g = comboBox1.CreateGraphics();  
         float maxWidth = 0f;
         foreach(object o in comboBox1.Items)  
              float w = g.MeasureString(o.ToString(), comboBox1.Font).Width;  
              if(w > maxWidth)  
                   maxWidth = w;
          comboBox1.Width = (int) maxWidth + 20; // 20 is to take care of button width
          /// comboBox1.DropDownWidth = (int) maxWidth

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now