473,395 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

text height problem

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.
Nov 20 '05 #1
10 2526
Cor
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
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?

Nov 20 '05 #2
I mean the line counts by wrap, not lines seperated by crlfs.

On Sat, 31 Jan 2004 09:54:12 +0100, "Cor" <no*@non.com> wrote:
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
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?


Nov 20 '05 #3
* liups <Ge*****@Spammers.com> scripsit:
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).

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #4
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.

On 31 Jan 2004 13:30:57 +0100, hi***************@gmx.at (Herfried K.
Wagner [MVP]) wrote:
* liups <Ge*****@Spammers.com> scripsit:
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).


Nov 20 '05 #5
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" <Ge*****@Spammers.com> wrote in message
news:qh********************************@4ax.com...
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.

On 31 Jan 2004 13:30:57 +0100, hi***************@gmx.at (Herfried K.
Wagner [MVP]) wrote:
* liups <Ge*****@Spammers.com> scripsit:
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).

Nov 20 '05 #6
Liups,

* liups <Ge*****@Spammers.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #7
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?
On 31 Jan 2004 17:51:26 +0100, hi***************@gmx.at (Herfried K.
Wagner [MVP]) wrote:
Liups,

* liups <Ge*****@Spammers.com> scripsit:
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.


Nov 20 '05 #8
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.

On 31 Jan 2004 17:51:26 +0100, hi***************@gmx.at (Herfried K.
Wagner [MVP]) wrote:
Liups,

* liups <Ge*****@Spammers.com> scripsit:
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.


Nov 20 '05 #9
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


On 31 Jan 2004 17:51:26 +0100, hi***************@gmx.at (Herfried K.
Wagner [MVP]) wrote:
Liups,

* liups <Ge*****@Spammers.com> scripsit:
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.


Nov 20 '05 #10
* liups <Ge*****@Spammers.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dr. Richard E. Hawkins | last post by:
I've googled around, and asked everyone I know, but I still can't find any reference to anyone else having faced this particular IE bug with floats. I've put a page at...
5
by: Harry | last post by:
CSS Description: Table contains a column of td cells with links. Achieved: 1. Entire link cells highlight when hovered. 2. The entire area of these cells are clickable. Problem:
0
by: NancyASAP | last post by:
In case anyone hasn't seen this problem, just sharing the info.... I created a dotnet 1.1 page with a literal control. I used a streamreader to open a text file to fill the control. I filled the...
0
by: VorTechS | last post by:
I'm having a problem with an inherited label, applying text rotation to aligned text. If text rotation is applied to the aligned text, the alignment goes 'nuts'. I can find no logic to what is...
3
by: =?Utf-8?B?SlIx?= | last post by:
I would like to add text to an image. I have tried to use DrawString and it works on some images but on others it is very very small. I am pretty sure it has something to do with the size of the...
4
by: pablorp80 | last post by:
Hello, Here is what I need: I need the focus and the cursor set to a textbox named txtGT, every time no matter if it is the first page load or whether it is a postback. Here is the problem: I...
16
by: Wayne | last post by:
I've read that one method of repairing a misbehaving database is to save all database objects as text and then rebuild them from the text files. I've used the following code posted by Lyle...
12
by: Keith G Hicks | last post by:
I have a client that I wrote an MS Access app for quite a few years ago where they need to know the height in inches of a block of text for billing their customers. The MS Access app opens Word in...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.