Part of my job includes maintaining an aging Winforms application which makes use of a set of components from Component1, which has been a mixed blessing. One problem we’ve come across repeatedly, but which I keep forgetting how to fix, has to do with either the visibility or the widths of columns in a combo box:
Typically, the issue is either:
- A column has disapeared, and needs to be set visible again, but I can’t remember how to do it, and documentation is hard to find.
- The widths of the internal columns need to be adjusted; that is, they may not match the drop-down area, which means there is either an empty area to the right of the columns, or part of the columns are outside the visible area.
The Solution: Splits
First, set the width of the whole drop-down area to whatever you need:
myComboBox.DropDownWidth = 200; // Set the with of the area
Now, to specify the visibility or width of the individual columns; this is the part I keep forgetting. You can’t set the with of a column directly, but you can do it via
DisplayColumns. Note that I’ve adjusted the column widths so that their sum do not add up perfectly to the same as the DropDownWidth. This is due to some padding between the columns, etc.
var split = myComboBox.Splits; split.DisplayColumns.Width = 20; split.DisplayColumns.Width = 56; split.DisplayColumns.Width = 56; split.DisplayColumns.Width = 56; // Note: This may only be necessary if the // columns were already set invisible earlier: split.DisplayColumns.Visible = true; split.DisplayColumns.Visible = true; split.DisplayColumns.Visible = true; split.DisplayColumns.Visible = true;
I haven’t dug into the technical details here, but from what I understood, all visible columns will typically be within the first split (ie. under myComboBox.Splits).
And that’s basically it: Just make sure Visible is set to true, and that Width for each DisplayColumn is appropriate, so that the sum of the widths of the columns match the total width of the ComboBox’s DropDownWidth.