I have a ToolStrip with a ToolStripComboBox control on it, and I would like it to autosize to fit the widest item in the drop down list. How can I accomplish that? The "Autosize" property is set to "true", but it doesn't seem to be making any difference. I've been banging my head over this for a while. Is it even possible?
A:
According to this msdn article AutoSize Property Overview only some of the controls support the AutoSize property. ComboBox has no AutoSize support.
volody
2010-04-29 21:39:13
That's not very helpful... Thanks MS.
rotard
2010-04-29 22:35:56
+1
A:
I had the same problem. My solution was to modify the size on the DropDown event. You can pass a max width in the MeasureString, or clamp maxWidth yourself before you set the DropDownWidth.
private void m_comboBox_DropDown(object sender, EventArgs e)
{
using (System.Drawing.Graphics graphics = CreateGraphics())
{
int maxWidth = 0;
foreach (object obj in m_comboBox.Items)
{
System.Drawing.SizeF area = graphics.MeasureString(obj.ToString(), m_comboBox.Font);
maxWidth = Math.Max((int)area.Width, maxWidth);
}
m_comboBox.DropDownWidth = maxWidth;
}
}
John Butterfield
2010-06-10 00:33:46