Results 1 to 2 of 2

Thread: [RESOLVED][2005] Determination of fixed width font size doesn't seem correct

  1. #1

    Thread Starter
    Junior Member Snizbatch's Avatar
    Join Date
    Sep 2002
    Location
    Jacksonville, FL
    Posts
    26

    Question [RESOLVED][2005] Determination of fixed width font size doesn't seem correct

    Hi guys,
    I have a Textbox that is being used to display a chunk of binary code from a portion of a file. It is a simple binary file preview with a Vertical Scroll Bar to change the position within a file. This allows one to open and view the binary contents of ANY sized file quickly by loading only what is to be displayed. All that works... But the simple task of determining how many characters across the textbox is capable of displaying is killing me. I want to know so I can load exactly the number of fixed-width characters that will fit in the sizable display. I'm using the Graphics.MeasureString method, and its returning a number that makes no sense when devided into the TextBox1.DisplayRectangle.Width property. I am assuming that it has to do with the .Unit property and scaling but I can't seem to get it right. Anyone got any ideas here?

    Jim

    Code:
            'The TextBox starts out at 262W x 264H pixels and DisplayRectangle 
            ' is 260 X 262 and the fixed width font being used is Lucida Console 9.75
            Dim g As Graphics = Me.CreateGraphics
            g.PageUnit = GraphicsUnit.Pixel
    
            'This MeasureString should return a number in the ballpark of 8
            '  I get 12.4012032
    
            Dim CharsWide as Integer = Me.txtFileDisplay.DisplayRectangle.Width / _   
               g.MeasureString("X",Me.txtFileDisplay.Font).Width / 2) 
            'The /2 is there because the program displays character, space, etc....
               
            'This MeasureString should return a number in the ballpark of 13
            '  I get 14.6249981
    
            Dim CharsHigh as Integer = Me.txtFileDisplay.DisplayRectangle.Height) /_   
               g.MeasureString("X", Me.txtFileDisplay.Font).Height))    '13
    
            ' The next line determines the total number of characters to load from      
            '   the file to fit nicely within the display area.
            Dim GetChars as Integer = CharsWide * CharsHigh
    Last edited by Snizbatch; Oct 23rd, 2007 at 02:31 AM. Reason: Resolved

  2. #2

    Thread Starter
    Junior Member Snizbatch's Avatar
    Join Date
    Sep 2002
    Location
    Jacksonville, FL
    Posts
    26

    Talking 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
  •  



Click Here to Expand Forum to Full Width