ComboBox width based on longest item

I have a ComboBox that I generate dynamically and fill with some items. I would like to set this control width to the width of the longest element. how to calculate the display width of some text?

edit: I am using windows forms, but I would like to do this in asp.net too

+1


source to share


3 answers


Depends. Are you using ASP.NET or Windows Forms or WPF? Are you using a fixed or proportional font?

If you are using Windows Forms, you will need to call MeasureString () to find out how much you need the text to be.



If you're using ASP.NET you can do something like MeasureString (), but you don't know exactly what font is displayed in the browser, so you can't just put it in your script.

+2


source


See the Graphics.MeasureString method . http://msdn.microsoft.com/en-us/library/9bt8ty58.aspx



+1


source


If you don't explicitly set the width, the browser will display it as the length of the longest element (unless the question is about web forms, of course).

+1


source







All Articles