Set the dropdown width of any combobox based on the data. Even if the data in the combo box is large, we don’t have to increase the width of the whole combo box to view the data.
Make sure combo box contains values before calling the below method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public int DropDownWidth(ComboBox comboBox) { int maxWidth = 0, temp = 0; foreach (var obj in comboBox.Items) { temp = TextRenderer.MeasureText(obj.ToString(), comboBox.Font).Width; if (temp > maxWidth) { maxWidth = temp; } } return maxWidth; } private void Form1_Load(object sender, EventArgs f) { //Set the combo box dropdown width based on the values comboBox1.DropDownWidth = DropDownWidth(comboBox1); } |