ToolStripComboBox - item automation

I have a ToolStrip with a ToolStripComboBox control, and I would like it to automatically sort to fit the widest item in the dropdown. How can i do this? The Autosize property is set to "true", but that doesn't seem to make any difference. I've been banging my head about this for a while. Is it possible?

+2


a source to share


2 answers


According to this msdn article, AutoSize Properties Overview, only a few controls support the AutoSize property. ComboBox does not support AutoSize.



0


a source


I had the same problem. My solution was to resize the DropDown event. You can pass the maximum width to MeasureString or clamp maxWidth yourself before setting 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;
    }
}

      

+6


a source







All Articles