Use MeasureString to determine required height


G

Guest

I'm trying to create a label control in VB.NET2005 that will automatically
change it's height to fit the content. To do that i'm using MeasureString to
determine required height for a set width.
What i found is that MeasureString sometimes returns the height which is
smaller then it should be and it seems to depended on the width.
So the question is am i doing something wrong or it's a bug. If it is a bug,
can someone recomend a reliable workaround.

Here is a code that used to test this. When width is set to 570, the last
line is not visible. When it's set to 590 all is OK.

Sub TestMeasureString(ByVal g As Graphics)
Dim s As Size
Dim f As Font = New Font("Arial", 11.25, FontStyle.Regular,
GraphicsUnit.Point)
Dim str As String = New String("W"c, 1000) & " END"

s = g.MeasureString(str, f, 570).ToSize
's = g.MeasureString(str, f, 590).ToSize
g.DrawRectangle(Pens.Black, New Rectangle(New Point(0, 0), s))
g.DrawString(str, f, Brushes.Black, New Rectangle(New Point(0, 0), s))
End Sub
 
Ad

Advertisements

S

Stoitcho Goutsev \(100\)

Alex,

If you are developing WindowsForms application using .NET 2.0 I'd suggest
using System.Windows.Forms.TextRenderer instead. This class is meant to be
used for measuring string on the screen only and uses GDI instead of GDI+,
thus is more accurate.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top