When at wit’s end for Combo-widths..

The problem

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:

  1. 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.
  2. 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[0];
split.DisplayColumns[0].Width = 20;
split.DisplayColumns[1].Width = 56;
split.DisplayColumns[2].Width = 56;
split.DisplayColumns[3].Width = 56;

// Note: This may only be necessary if the
// columns were already set invisible earlier:
split.DisplayColumns[0].Visible = true;
split.DisplayColumns[1].Visible = true;
split.DisplayColumns[2].Visible = true;
split.DisplayColumns[3].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[0]).

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.