text height problem

  • Thread starter Thread starter liups
  • Start date Start date
L

liups

Hi,
I have a multi-line textbox that accepts text input, I want it to
adjust its height according to the text it holds, so I use a
SendMessage API to get the line count of the text, then set the
TextBox.Height to (linecount * font.Height), but there is something
wrong with this calculation, the textbox is always larger than the
text lines, any ideas?
Thank you.
 
Hi Liups,

To get the line count of a textbox you do not need an Api, that is full
supported with managed code.

But with lines in a textbox is ment a line closed by a carriage return
(CR).

(To get the row count in a textbox I do not know managed code).

Is that maybe the problem?

Cor
 
* liups said:
I have a multi-line textbox that accepts text input, I want it to
adjust its height according to the text it holds, so I use a
SendMessage API to get the line count of the text, then set the
TextBox.Height to (linecount * font.Height), but there is something
wrong with this calculation, the textbox is always larger than the
text lines, any ideas?

You can use 'Graphics.MeasureString' to get the size of the string
(approximately).
 
Thank you Herfried,
I tried that and the got same problem, the result of
'g.measurestring(str, font, DesiredWidth).height' is still not the
right textbox height, it's always a little larger.
 
liups,
Use one of the overloads that uses a StringFormat object and use a copy
of the StringFormat.GenericTypographic object to get closer measurements.
The stock StringFormat adds some boundary space all around the string. For
more information search for MeasureString in
microsoft.public.dotnet.framework.drawing.

Ron Allen
 
Liups,

* liups said:
I tried that and the got same problem, the result of
'g.measurestring(str, font, DesiredWidth).height' is still not the
right textbox height, it's always a little larger.

You can create the 'Graphics' object using the control's
'CreateGraphics' method. When you don't need the 'Graphics' object any
more, don't forget to call its 'Dispose' method.

Did you play around with the different overloaded versions of
'MeasureString'? There is at least one overloaded version that expects
a parameter of type 'StringFormat'. Try to pass
'StringFormat.GenericTypographic' there.
 
Thank you Herfried and Ron,

Dim sFormat As StringFormat = New _
StringFormat(StringFormat.GenericTypographic)
RequiredHeight = CInt(g.MeasureString(Me.txtTextBox.Text, fnt, _
Me.txtTextBox.Width, sFormat).Height)

The height is still not right, and there is one more thing, when I try
to enter some text in it, when its height grows, the cursor is not at
the end of the line, sorry for my english but it's natural for the box
to grow when the cursor is at the end of one line, that means when the
text wraps to next line, the textbox grows in height, but now, the
box's height grows when the text is not wrapping.
It seems that the width MeasureString is using is not the same with
the width I passed to it, or maybe it's because I'm using Asian Chars
in it? is there a 'wide' version for this?
 
There is a problem with this method, when the textbox is empty, and
the user press ENTER, the textbox should grow its height, but it
doesn't, it grows when the use press ENTER again.
 
This morning I did this for further experiment, I tried to draw the
rectangle that MeasureString calculated onto the textbox, to see how
it fits the real text, the code I use is as follows, from this test it
seems MeasureString is always getting a larger height that the real
text height, the difference is very noticeable if there are more than
10 lines.
Any ideas for this experiment? Is there another way to get the height
of some lines of text?
Thank you very much.

Public Class Form1
Inherits System.Windows.Forms.Form
Dim brsh As Brush = New SolidBrush(Color.Black)
Dim pen As pen = New pen(brsh, 1)
Dim g As Graphics
Dim rect As Rectangle
Dim sf As StringFormat = New _
StringFormat(StringFormat.GenericTypographic)

Public Sub New()
....
g = TextBox1.CreateGraphics
g.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
rect = New Rectangle(0, 0, TextBox1.Width, TextBox1.Height)
End Sub

Private Sub TextBox1_TextChanged ...
Invalidate()
End Sub

Protected Overrides Sub OnPaint ...
Dim s As SizeF
s = e.Graphics.MeasureString(TextBox1.Text, TextBox1.Font, _
TextBox1.Width, sf)
rect.Width = s.Width
rect.Height = s.Height
g.DrawRectangle(pen, rect)
End Sub
 
* liups said:
This morning I did this for further experiment, I tried to draw the
rectangle that MeasureString calculated onto the textbox, to see how
it fits the real text, the code I use is as follows, from this test it
seems MeasureString is always getting a larger height that the real
text height, the difference is very noticeable if there are more than
10 lines.

The Windows Forms textbox isn't drawn using .NET drawing functions, it's
drawn by Windows directly. That's maybe why you don't get the exact
values... I feel sorry, but I don't know a better solution.
 
Back
Top