Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listbox text width

Status
Not open for further replies.

sjc

IS-IT--Management
Jun 7, 2001
41
GB
Hello All,

I have a single colunmed list box and I'm adding long strings to it. What I want to know *before* I add the string to the list box is whether the string is too long to show it completely in the listbox.

You obvioulsy can't compare the ClientWidth with the string.Length as they are in different measurements.

Is there some equivalent to Canvas.TextExtent() or something similar ?

Thanks
sjc
 
ok I worked it out.

//first I grabbed the graphics object of the listbox
Graphics g = lstFiles.CreateGraphics();

// next created a SizeF struct
SizeF fs = new SizeF(0, 0);

// Then used the MeasureString method of the graphics
// object passing the string I was interested in and the
// font of the listbox. This gave me the width which I
// could then compare to the listbox.ClientSize.Width

fs = g.MeasureString(filename, lstFiles.Font);

if (fs.width > lstFiles.ClientSize.Width)
etc etc

I have no idea if this is the best way but it works for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top