MeasureString help

wcsloco

Member
Joined
Feb 7, 2005
Messages
14
Programming Experience
5-10
I'm trying to find the best way to get the length of each line in a multi-line textbox. I'm trying to use
MeasureString
, but I'm not having much luck. Does anyone have any examples of using MeasureString, using the returned value of the
linesFilled
parameter? I just want to pass it the dimensions of my textbox and find out when it wraps. Thanks in advance.
 
Measure string returns a sizeF object... to implement..

VB.NET:
Dim G as graphics = me.createGraphics
Dim StringSize as new SizeF
 
StringSize = g.measurestring('the string to measure', the font in the textbox)
 
' Remember to dispose of the graphics object
 
g.dispose





you then have...

VB.NET:
Stringsize.width // equals the width of the string
 
StringSize.Height // then height of the string


To get the length of each string in the textbox...

VB.NET:
Dim StrSize as SizeF
Dim StrLength(Textbox.lines.length-1) as integer 
Dim G as graphics = me.creategraphics
For i as integer = 0 to textbox.lines.count-1
StrSize = g.measurestring(textbox.lines(i),textbox.font)
strlength(i) = strsize.width
next
g.dispose
It's a bit late here but i hope that is what you are after.
 
Thank you for the lengthy and helpful reply. Sadly, when using your code, I end up with the same output as what I originally had. What's confusing is that my textbox is 40 x 304, and the width of my test string is 326. How can that be? Here are the details:

FONT - Microsoft Sans Serif, Regular, 8

textbox height 40
textbox width 304

Client Rectangle height 36
Client Rectangle width 300

Here is the text I input, to completely fill the first line without wrapping

This is line 1asldfj slkdjf dslkfj dslkjf sdlkfj dslkfj ldskjf dslkfj i ii i i i

And when I use your code (and mine) I get a width of 326. I wouldn't think the width could ever be more than 300 (or 304 at the max). What am I missing?
 
Hi ya, sorry that took so long, busy day at work. I've got the solution for you and it was as i expected.
It is measuring the string properly it's just that when you get the textbox.width it includes
the size of the borders, so we have to minus them from our final figure. It goes like this....

VB.NET:
Dim g As Graphics = Me.CreateGraphics
Dim TxtSze As Size
Dim BorderSze as integer = SystemInformation.Border3DSize.Width *2
TxtSze = Size.Ceiling(g.MeasureString(Me.TextBox1.Lines(0), Me.TextBox1.Font, 
Me.TextBox1.Width - BorderSze, System.Drawing.StringFormat.GenericDefault))
G.dispose

Now if you fill a line with characters and do a messagebox to display:

VB.NET:
TxtSze.width
Textbox1.Width-BorderSze
They will be equal.
 
To add, you can also use the ClientSize property: TextBox1.ClientSize.Width

Control.ClientSize Property: Gets or sets the height and width of the client area of the control. The client area of a control is the bounds of the control, minus the nonclient elements such as scroll bars, borders, title bars, and menus.
 
Back
Top