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

Re: Use MeasureString to determine required height by Dmytro

Dmytro
Fri Apr 07 02:18:26 CDT 2006

Hi Alex,

Hopefully this article will provide some insight...

http://support.microsoft.com/kb/307208/en-us

"Alex Broide" <AlexBroide@discussions.microsoft.com> wrote in message
news:1EC9DBDD-A255-40FE-874F-D8FBDE78C3D9@microsoft.com...
> 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


Re: Use MeasureString to determine required height by Stoitcho

Stoitcho
Fri Apr 07 08:25:48 CDT 2006

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.


--
HTH
Stoitcho Goutsev (100)

"Dmytro Lapshyn [MVP]" <x-code@no-spam-please.hotpop.com> wrote in message
news:%23%233w3NhWGHA.3848@TK2MSFTNGP05.phx.gbl...
> Hi Alex,
>
> Hopefully this article will provide some insight...
>
> http://support.microsoft.com/kb/307208/en-us
>
> "Alex Broide" <AlexBroide@discussions.microsoft.com> wrote in message
> news:1EC9DBDD-A255-40FE-874F-D8FBDE78C3D9@microsoft.com...
>> 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
>



Re: Use MeasureString to determine required height by AlexBroide

AlexBroide
Sat Apr 08 20:55:01 CDT 2006

Thank you Stoitcho,

TextRenderer worked very nicely.

Alex

"Stoitcho Goutsev (100)" wrote:

> 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.
>
>
> --
> HTH
> Stoitcho Goutsev (100)
>
> "Dmytro Lapshyn [MVP]" <x-code@no-spam-please.hotpop.com> wrote in message
> news:%23%233w3NhWGHA.3848@TK2MSFTNGP05.phx.gbl...
> > Hi Alex,
> >
> > Hopefully this article will provide some insight...
> >
> > http://support.microsoft.com/kb/307208/en-us
> >
> > "Alex Broide" <AlexBroide@discussions.microsoft.com> wrote in message
> > news:1EC9DBDD-A255-40FE-874F-D8FBDE78C3D9@microsoft.com...
> >> 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
> >
>
>
>