473,408 Members | 2,832 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,408 software developers and data experts.

problem with measureString in textBox


Hi,
I'm having problems with the measureString function. I am trying to
measure the length of a string the user types into a text box. However,
the size in pixels that this function returns is different than what is
displayed on screen. This causes a visual overflow from the text box
rectangle for some inputs.

I've tried using measureCharacterRanges instead and the same problem
still arises. A partial solution is supplied by using the function
getTextExtentPoint32. Using getTextExtentPoint32 no input overflows, but
some inputs are truncated before the end of the text box.

Do you know of any other function that will supply a full solution?

Thanks,
Shimrit


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
8 5946
Shimrit,

How are you validating the length that the function returns versus what
is on screen?

You say that this also causes a "visual overflow". What exactly is
that? Are you custom-drawing the textbox? If so, what is the effect you
are trying to achieve? What are you comparing the number against?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Shimrit Ben-Yair" <sh*******@yahoo.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...

Hi,
I'm having problems with the measureString function. I am trying to
measure the length of a string the user types into a text box. However,
the size in pixels that this function returns is different than what is
displayed on screen. This causes a visual overflow from the text box
rectangle for some inputs.

I've tried using measureCharacterRanges instead and the same problem
still arises. A partial solution is supplied by using the function
getTextExtentPoint32. Using getTextExtentPoint32 no input overflows, but
some inputs are truncated before the end of the text box.

Do you know of any other function that will supply a full solution?

Thanks,
Shimrit


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
First, make sure you are using
System.Drawing.StringFormat.GenericTypographic
when measuring the string.

Otherwise, what font are you using?

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Shimrit Ben-Yair" <sh*******@yahoo.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...

Hi,
I'm having problems with the measureString function. I am trying to
measure the length of a string the user types into a text box. However,
the size in pixels that this function returns is different than what is
displayed on screen. This causes a visual overflow from the text box
rectangle for some inputs.

I've tried using measureCharacterRanges instead and the same problem
still arises. A partial solution is supplied by using the function
getTextExtentPoint32. Using getTextExtentPoint32 no input overflows, but
some inputs are truncated before the end of the text box.

Do you know of any other function that will supply a full solution?

Thanks,
Shimrit


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3
Hi Nicholas,

I'm trying to get string input from the user to fit into a textbox. I
want to allow writing strings into this textBox, until a certain length
(the textbox width).

I'm validating the length that the function returns using the
measureString / measureCharacterRanges return values. This gives me the
length of the string the user has typed in.

For example:
A string "aaaaa" that is 20 pixels long, and a string "www" that is 10
pixels long. The problem arises when the "www" string, that is shorter
in pixels, looks longer on the screen than the "aaaa" string.

I check stringLength versus textBoxLength to see if stringLength has
exceeded textBoxLength. If it has, i do not want to print it on the
screen. So, although in pixels the string fits in the textBox and passes
the check, visually it does not and it overflows.

The function getTextExtentPoint32 gives the opposite trouble. It does
measure the string properly, not allowing an overflow, only now it
truncates some inputs in the middle.

Thanks, Shimrit

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
Hi Dennis,

I've tried using System.Drawing.StringFormat.GenericTypographic and it
refines the measurement by a few pixels but in total - still not good
enough.

Also, I'm not using a specific font, this happens in all the fonts that
I have checked.

Thanks, Shimrit

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5
shimrit wrote:
[...]
I check stringLength versus textBoxLength to see if stringLength has
[...]


Maybe this help you: there is a two pixel margin around the internal area of
the text box; are you taking this in account when you compute textBoxLength?

Massuda
Nov 16 '05 #6
Hi Massuda,

measureString / measureCharacterRange / getTextExtentPoint32 are
measuring the pixel length correctly. The inconsistency is with what is
shown on the screen, that doesn't match these return values.
For example - a string that is visually short gets a bigger pixel value
than another string that is visually longer.

Thanks,
Shimrit
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #7
shimrit wrote:
measureString / measureCharacterRange / getTextExtentPoint32 are
measuring the pixel length correctly. The inconsistency is with what
is shown on the screen, that doesn't match these return values.
For example - a string that is visually short gets a bigger pixel
value than another string that is visually longer.


Maybe I am wrong, but you said that you "check stringLength versus
textBoxLength"; my guess is that you are using textBox.Width as the
textBoxLength value, but this is wrong... if you have Width =100 pixel, your
text box internal (client) area width will be 96 (or less) pixels.

Since you are checking stringLength against a value which is greater than
the real value, in some cases, a string which your check decide it is short
is in fact longer than the available internal text box space.

Massuda
Nov 16 '05 #8
Hi Shimrit:

Another tip in this article suggests "Set the Graphics
TextRenderingHint to TextRenderingHintAntiAlias".
http://windowsforms.net/articles/gdiptext.aspx

I've done something similar and it was working well for me, YMMV.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Wed, 13 Oct 2004 08:10:58 -0700, shimrit <sh*****@devdex.com>
wrote:
Hi Dennis,

I've tried using System.Drawing.StringFormat.GenericTypographic and it
refines the measurement by a few pixels but in total - still not good
enough.

Also, I'm not using a specific font, this happens in all the fonts that
I have checked.

Thanks, Shimrit

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 16 '05 #9

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

Similar topics

2
by: DotNetJunkie | last post by:
Hi NG, I want to pass a very long string to a multilined TextBox. After doing this, I want to resize the TextBox vertically, so that the text fits into the TextBox exactly. Any ideas? I need it...
1
by: Kyle Blaney | last post by:
When labels are vertically stacked on top of one another and have their TextAlign property set to MiddleRight, the text is not properly right-aligned. You can reproduce this problem by creating...
1
by: Yaron | last post by:
Hi, I made a procedure which receives a Graphics parameter, and with that Graphics parameter, performs various things. The area of confusion here is that when I pass the procedure a Graphics...
2
by: Stuart Norris | last post by:
Dear Group, I have a question about the result from ‘graphics.MeasureString' when using a mono-spaced font. Firstly I assume that Courier New font is mono-spaced, ie each character takes up the...
0
by: keith | last post by:
I did a test typing characters until fill out whole textbox (single line). Then I chcked width of this string in paint event e.Graphics.MeasureString(textBox1.Text,textBox1.Font).Width e.g....
10
by: liups | last post by:
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...
1
by: ramhog | last post by:
Hello all, I am trying to resize a textbox to exactly fit the contents. The method I am using gets it close but it is just a little bit too small. Could someone tell me where I am going wrong...
2
by: teo | last post by:
I have to measure the length of a string in a Label, to set the adequate Label width (because not all browsers support the auto-size Label property). I decide to use the 'MeasureString'...
6
by: Rainer Queck | last post by:
Hello NG, I am trying to adjust the column width of a DataGridView to the Text in its column header. To do this I am using the Graphics.MeasureString method. Unfortunatly I alway get a too...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.