.net - How to I find the space a combo box needs to show all list items -


I have a WinForms combo box, I want to set the width of the column box so that any selected item is shown Maybe full (I do not know what items will be in the combo box when writing software)

However, when I call on the combo box. Delegated size, it does not appear that the item drop down list.

You can use the System.Drawing.Graphics.MeasureString method For more information on how to find the most detailed item in the list, see.

  Private Zero ResizeComboBox (ComboBox comboBox) {var maxItemLength = 0; // A graphics object to use the combo box: (var g = Graphics.FromHwnd (comboBox.Handle)) {foreach (comboBox.Items.Cast> var item in string} (var itemLength = G.MeasureString (item, comboBox.Font); MaxItemLength = Math.Max ​​((int) itemLength.Width, maxItemLength); }} // correction for the drop down arrow maxItemLength + = 15; ComboBox.Width = maxItemLength; }  

Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -