Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5931477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:42:01+00:00 2026-05-22T14:42:01+00:00

Of course, the Graphics.MeasureString method is well-known to have padding problems, and so you

  • 0

Of course, the Graphics.MeasureString method is well-known to have padding problems, and so you use Graphics.MeasureCharacterRanges instead, but as you can see here:

MeasureCharacterRanges problem

It’s not measuring quite correctly. Is this a problem with MeasureCharacterRanges or is it my code? How can I fix it?

Here’s my code:

    'Draw the selection cursor
    If Me.Focused Then
        Dim cX As Integer = 1, cY As Integer = 5, c As Char
        For i As Integer = 0 To Me.SelectionStart - 1
            c = Me.Text(i)

            If c = ControlChars.CrLf Then
                cY += Me.Font.Height
            Else
                Dim w As Integer = MeasureCharacter(g, c, Me.Font).Width
                g.DrawRectangle(Pens.Black, cX, cY, w, Me.Font.Height) 'Draw a rectangle for debugging
                cX += w
            End If
        Next

        g.DrawLine(Pens.Black, cX, cY, cX, cY + Me.Font.Height)
    End If
End Sub

Protected Function MeasureCharacter(ByVal g As Graphics, ByVal c As Char, ByVal f As Font) As Size
    Dim cr() As CharacterRange = {New CharacterRange(0, 1)}
    Dim sfmt As New StringFormat()
    sfmt.FormatFlags = StringFormatFlags.MeasureTrailingSpaces
    sfmt.SetMeasurableCharacterRanges(cr)
    Return g.MeasureCharacterRanges(c.ToString(), f, Me.ClientRectangle, sfmt)(0).GetBounds(g).Size.ToSize()
End Function
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:42:01+00:00Added an answer on May 22, 2026 at 2:42 pm

    Measuring the length of indiviual characters doesn’t take into account the kerning that occurs between groups of characters, so the sum of the lengths of the characters will not equal the length of the string.

    If you look at your example text, you can see the kerning between the “t” at the end of “Sprint” and the “T” at the start of “Textbox”, the characters are moved closer together than you would expect given their individual lengths.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm starting a computer graphics course, and I have to choose a language. Choices
Of course most languages have library functions for this, but suppose I want to
Of course, I can explain it in whole books. But I read a few
Of course we can do this, but is it alright to do so? Are
I am taking Computer Graphics course.In 3D,I have a point and a polygon and
I've written an method that does some graphics calculations. There, you can specify a
This semester, I took a course in computer graphics at my University. At the
Of course I am aware of Ajax, but the problem with Ajax is that
In my book (for my computer science course) they have source code snippets. We're
I see answers to questions suggesting the use of BufferedImages for performing increases that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.