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:
- 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).
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;