SDF Sample: Showing the Entire String Contents of a ComboBox DropdownList

Following Chris’ SDF Samples (here, here and here) here is another little tip that reminded me we had this functionality in the Smart Device Framework while talking to a customer facing this issue.

I have a combobox with the following items in the list:

  1. Red
  2. Green
  3. A very dark and drab Gray

When you run your application you get something like this:

Your users call your helpdesk (and if you are like us the developers are the help desk :)) and complain they can’t see the last colour option.

[Tested using Windows Mobile 5 emulator and Smart Device Framework 2.1]

Two possible solutions to this.  First, manually extend the ComboBox via the Visual Studio designer.  This is fine, but what happens if you create forms dynamically (as many of our customer do).

Second solution is found in OpenNETCF.Windows.Forms.ComboBox2 class and the DropDownWidth property.  Setting this property you get this:

Now if you have a dynamic form you can use this helper method to dynamically size the DropDownWidth.

private void AutoSizeDropDownWidth(OpenNETCF.Windows.Forms.ComboBox2 comboBox)
    int margin = 10;
    using (Graphics g = this.CreateGraphics())
        SizeF size = SizeF.Empty ;
        int largestWidth = comboBox.Width + margin;
        for (int x = 0; x < comboBox.Items.Count; x++)
            size = g.MeasureString(this.comboBox21.Items[x].ToString(), this.comboBox21.Font);
            largestWidth = ((int)size.Width>largestWidth ? (int)size.Width + margin: largestWidth );
        comboBox.DropDownWidth = largestWidth;

Warning: count(): Parameter must be an array or an object that implements Countable in /nfs/c07/h02/mnt/110221/domains/ on line 405

Leave a Reply