TextWidth

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In VB6 there was a function for the WindowsForm called TextWidth. This function when called passing a string you would return the width of the string as it will be displayed on your screen. With this I was able to appropriately set the controls width property with the contents of its text property. Making the text to be displayed in its entirety without being cutoff.

Is there a VB.NET equivalent? I looked around for this function and it no longer exists. Is there a system class that would do the same for me

Thanks...
 
Hi, Angel

you need to check up Graphics.MeasureString method.

HTH
Alex

Angel said:
In VB6 there was a function for the WindowsForm called TextWidth. This
function when called passing a string you would return the width of the
string as it will be displayed on your screen. With this I was able to
appropriately set the controls width property with the contents of its text
property. Making the text to be displayed in its entirety without being
cutoff.
Is there a VB.NET equivalent? I looked around for this function and it no
longer exists. Is there a system class that would do the same for me.
 
Hi Angel,

As Alex says, Graphics.MeasureString will work in most cases, but it does
not return the exact size. It adds a little bit of extra space around it,
so it will return at least the size of the string, given the font and
graphics object.

To get the exact measurements, you need to use
Graphics.MeasureCharacterRanges

Happy coding!
Morten Wennevik [C# MVP]
 
Back
Top