I understand that. However, using MeasureString, you can get the width of a string when rendered with a specific font.
What I want is given a specific width, how much of a specific (it changes, but you will always know what it is) string can be rendered in that space.
As a previous poster suggested, calling MeasureString for an ever increasing number of characters from the string is one way (which I am currently doing), I just wondered if there was some method I had overlooked.
The reason I am doing this is because I need to know which character in a rendered string the user clicks on with a mouse.
Thanks for your help though,
Chris
Kevin Spencer said:
I have an arbitrary string value and wish to determine how many characters
from that string will fit in a specified area with a specific font when
drawn.
With all fonts except for fixed-width fonts, the width of a character is different for each character. If one is working with an unknown set of characters, it is therefore impossible to pre-determine how many of any combination of them will fit into an area. That would be like guessing how many numbers add up to 100. Consider the illustration below:
R a N D o M s T r I n G
7 6 7 8 5 9 5 8 4 1 5 9
I measured each character in pixels on my machine. Now, tell me, without using this particular string, how many Arial 10 point characters could fit into an area 100 pixels wide? If they were all "I" you could fit nearly 100 of them (you would have to subtract for spacing). If they were all "M" you could only fit about 9.
That is why the Graphics.MeasureString method must have a String to measure.
So, knowing that this is true, you need to take a fresh look at your requirement, and find a different way of fulfilling it.
--
HTH,
Kevin Spencer
Microsoft MVP
.Net Developer
Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.