problem with measureString in textBox

  • Thread starter Thread starter Shimrit Ben-Yair
  • Start date Start date
S

Shimrit Ben-Yair

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
 
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?
 
First, make sure you are using
System.Drawing.StringFormat.GenericTypographic
when measuring the string.

Otherwise, what font are you using?
 
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
 
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
 
shimrit said:
[...]
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
 
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
 
shimrit said:
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
 
Back
Top