views:

200

answers:

2

To make sure that the text in listbox items is not truncated I want to adjust its width to the width of the item with longest text.

Is there any way to get a "actual" width of the listbox item the one which is based on the item's text.

ListBox.GetItemRectangle does not seem to be returning this actual width.

thank you.

+2  A: 

Check out the MeasureString method.

Gerrie Schenck
+2  A: 

MeasureString method: (copy pasted from msdn)

Graphics g = control.CreateGraphics();
int width = (int)g.MeasureString(aString, control.Font).Width;
g.dispose();

Add width of the textbox (left and right side) to the value

PoweRoy