[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Combo Box with text longer than the control

Posted on 2005-04-05
5
Medium Priority
?
230 Views
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?
0
Comment
Question by:jjacksn
2 Comments
 
LVL 11

Accepted Solution

by:
Jokra_the_Barbarian earned 1000 total points
ID: 13711166
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);
}      
0
 
LVL 6

Assisted Solution

by:melodiesoflife
melodiesoflife earned 1000 total points
ID: 13715207
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;
      }
      g.Dispose();
      comboBox1.Width = (int) maxWidth + 20; // 20 is to take care of button width
      /// comboBox1.DropDownWidth = (int) maxWidth
 
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

834 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