Line length

  • Thread starter Thread starter Patrick de Ridder
  • Start date Start date
P

Patrick de Ridder

I read in a text file;
trim the lines;
establish the lengths of the lines.

Measured lengths bear no relationship to actual lengths;
the listBox uses a true type.
What is the cause of this?
How can this be overcome?
Thank you,
Patrick.
 
Hi Patrick,

I assume you are referring to Graphics.MeasureString(). It is a fast,
simple, but unreliable method to quickly calculate the length of a
string. MeasureString will report a size slightly larger than the actual
space taken by the string.

The method adds extra space to "optimize display quality", so if you put
several strings after eachother you will find some space between the
strings. If you need exact measurements you need to use
Graphics.MeasureCharacterRanges() which is a whole lot more complex, and
more sensitive to resolution.

Happy coding!
Morten Wennevik [C# MVP]
 
Hi Morten,
Actually I was just doing len = blabla.Length;
Patrick

Hi Patrick,

I assume you are referring to Graphics.MeasureString(). It is a fast,
simple, but unreliable method to quickly calculate the length of a
string. MeasureString will report a size slightly larger than the actual
space taken by the string.

The method adds extra space to "optimize display quality", so if you put
several strings after eachother you will find some space between the
strings. If you need exact measurements you need to use
Graphics.MeasureCharacterRanges() which is a whole lot more complex, and
more sensitive to resolution.

Happy coding!
Morten Wennevik [C# MVP]
 
Patrick de Ridder said:
Actually I was just doing len = blabla.Length;

That gives the length of the string in characters.

Was that what you were after? If so, could you give an example of how
the observed length was different to the actual length?
 
In that case, the Length property of a string reports the number of
characters, not how long the string appears on the screen (which is
dependent on font and screen resolution).

String.Trim() will remove blank characters like space, tab, carriage
return, new line at the beginning and the end of the string (note you need
to use newstring = oldstring.Trim()) because the original string will
never be changed.

For a truetype font,

string text1 = "iiiiiiiiii";
// text1.Length = 10, but width is 29.5 units with default font

string text2 = "mmmmmmmmm";
// text2.Length = 9, but width is 88.5 units with default font

but text1 will appear shorter on screen because the truetype has variable
widths for different characters and i is very thin.

If you want the strings to be the same equivalent width as length you need
to use a fixed size font, like Courier New.


Happy coding!
Morten Wennevik [C# MVP]
 
Hi,

Thanks, yes, String.Len counts characters.

How do find out which fonts are true type besides courier new?

Patrick.
 
To find out if the current font is truetype, or monotype, you need to
measure the size of two characters that would be different sized in
truetype, like 'i' and 'm'

string i = "iii";
string m = "mmm";

Graphics g = MyControl.CreateGraphics();
if(g.MeasureString(i, MyControl.Font) != g.MeasureString(m,
MyControl.Font))
// truetype
else
// monotype
g.Dispose();


Happy coding!
Morten Wennevik [C# MVP]
 
Hi Morten,
I ended up trying them *all* out, saw them, and ... did
without a straight edge in the end, not all that cool anyway.
Thank you for your responses.
Patrick.



To find out if the current font is truetype, or monotype, you need to
measure the size of two characters that would be different sized in
truetype, like 'i' and 'm'

string i = "iii";
string m = "mmm";

Graphics g = MyControl.CreateGraphics();
if(g.MeasureString(i, MyControl.Font) != g.MeasureString(m,
MyControl.Font))
// truetype
else
// monotype
g.Dispose();


Happy coding!
Morten Wennevik [C# MVP]
 
Back
Top