|
-
Oct 18th, 2007, 08:38 PM
#1
Thread Starter
Junior Member
Last edited by Snizbatch; Oct 23rd, 2007 at 02:31 AM.
Reason: Resolved
-
Oct 23rd, 2007, 02:31 AM
#2
Thread Starter
Junior Member
Re: [2005] Determination of fixed width font size doesn't seem correct
I never got any response from anyone about my problem, but my determination paid off...
I was trying to figure out how many FIXED WIDTH characters would fit across a textbox or other control with the DisplayRectangle property. I got the wrong number whenever I used the measurestring method, so I gave up on it and pursued the TextRenderer.MeasureText method instead. It actually worked. The result was still off because there are leading and trailing spaces in the rendered text string, but that is easily solved by subtracting the length of a one character string from the length of a two character string. The result is the length of the additional character.
One additional problem I ran into was that the DisplayRectangle property seems to be padded by 2 or 3 characters depending on the size font you use. I'm sure there's an explanation, I just can't find it. The solution is to subtract the 2 or 3 from DisplayRectangle prior to dividing by the font width so you get an integer result. Truncating the result removes the decimal caused by any extra spaces less than the width of one character in DisplayRectangle.
Here's my code:
Code:
Dim charwidth As Integer = TextRenderer.MeasureText("00", Me.TextBox1.Font).Width - TextRenderer.MeasureText("0", Me.TextBox1.Font).Width
CharsWide = CInt(Math.Truncate(Me.TextBox1.DisplayRectangle.Width - 2) / charwidth)
CharsHigh = CInt(Math.Truncate((Me.txtFileDisplay.DisplayRectangle.Height - 2) / Me.txtFileDisplay.Font.GetHeight))
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|