Question

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?

Was it helpful?

Solution

According to this msdn article AutoSize Property Overview only some of the controls support the AutoSize property. ComboBox has no AutoSize support.

OTHER TIPS

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;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top