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.

Problem
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.

Solution
[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;
    }
}


Wednesday, April 25, 2007 4:04:26 PM (Eastern Daylight Time, UTC-04:00) #    Comments [0]  | 

 

All content © 2008, Mark Arteaga
Related Sites
Archives
Sitemap
Disclaimer

Powered by: newtelligence dasBlog 1.9.7174.0

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts