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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Measuring string width, BUT no PaintEventArgs 2

Status
Not open for further replies.

groston

IS-IT--Management
Dec 31, 2001
141
0
0
US
In my application, I get a string of data from a user. I need to determine the length of the string, because if it is longer than some predetermined length, I need to insert a carriage return so that it fits inside a fixed width box on a PDF form.

All of the methods I have seen for calculating string width depend on the PaintEventArgs method that is exposed by Form or Control's Paint event. In this case, I do not have access to this event as the string is not be painted.

Can you please offer a solution?

Thanks.


----

Gerry Roston
gerry@pairofdocs.net
 
Sorry - small error in post -

All of the methods I have seen for calculating string width depend on the Graphics.MeasureString method exposed by the PaintEventArgs parameter of a form or control's Paint event.


----

Gerry Roston
gerry@pairofdocs.net
 
I don't know of any other way to get the exact length of a string -- to do so you need to know the display attributes (thus the Graphics class).

However, what you can do is use the .Size property of the Font object. This is the "em" size of the font, which is the width of the lower-case "m" character. That's typically regarded as an "average width" for a font. If your string is all-caps, or all-lowercase, it may not apply. But for a typical sentence it's reasonably close.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
You can use the graphics object exposed by the form (or any other control). The following procedures are located in a Form Class:

Code:
Public Function GetStringWidth(ByVal sStr As String, _
                               ByVal fFont As Font) _
                               As Single
    Dim g As Graphics = Me.CreateGraphics
    Dim TextWidth As Single
    TextWidth = g.MeasureString(sStr, fFont).Width()
    'Clean up
    g.Dispose() : g = Nothing
    Return TextWidth
End Function

Public Function GetStringSizeF(ByVal sStr As String, _
                               ByVal fFont As Font) _
                               As SizeF
    Dim g As Graphics = Me.CreateGraphics
    Dim TextSizeF As SizeF
    TextSizeF = g.MeasureString(sStr, fFont)
    'clean up
    g.Dispose() : g = Nothing
    Return TextSizeF
End Function
 
Paszt,

Thanks for the assistance.

The answer was not exactly as you specified - in ASP.NET, the call Me.CreateGraphics doesn't get you anywhere as a Page object does not suport this method.

What worked for me was:

Dim objBitmap As Bitmap = New Bitmap(1000, 1000)
Dim g As Graphics = Graphics.FromImage(objBitmap)

and from there, everything else was the same.

----

Gerry Roston
gerry@pairofdocs.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top