Monday, January 28, 2008

Windows forms: Adjust width of combobox drop down according to content

Combobox control has the DropDownWidth property to control the width of the dropped portion that appears when the user clicks the drop down arrow. This property takes the width in pixels.

It's nice to have the width of the drop down set according to the contents. This code does the job:
Assuming the name of the control is cmb:

int ItemMaxWidth = this.cmb.DropDownWidth;
Graphics gx = cmb.CreateGraphics();

for (int i = 0; i < this.cmb.Items.Count; i++)
//get the width of item
SizeF s = gx.MeasureString(cmb.Items[i].ToString(), cmb.Font);
if ((int)s.Width > ItemMaxWidth)
ItemMaxWidth = (int)s.Width;


//Set the width :
cmb.DropDownWidth = ItemMaxWidth;

Tuesday, January 1, 2008

List of articles I read on 2007

This is a list of most of the articles I read in year 2007, I wish you find some of them interesting.