How to Calculate Width of a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bottomupthinker
    New Member
    • Mar 2008
    • 1

    How to Calculate Width of a string

    Hi all
    i have a string cotaining numeric values also.
    the font is courier New
    now i want to calculate its width
    please send the code
  • jamesd0142
    Contributor
    • Sep 2007
    • 471

    #2
    Originally posted by bottomupthinker
    Hi all
    i have a string cotaining numeric values also.
    the font is courier New
    now i want to calculate its width
    please send the code
    Im a little confused, do you mean the length of the string?

    Comment

    • kenobewan
      Recognized Expert Specialist
      • Dec 2006
      • 4871

      #3
      Have you tried len(string)?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        You really should have at least TRIED this on your own.
        If you want the width of the string in a given font, you have to decide on a few other details about the font. (Size, bold? italic? etc)
        You can use the measure string on a graphics object to tell you it's width.
        I think some web-gui controls also have this ability, but I can't remember them offhand.

        Code:
        Graphics g = Graphics.FromImage(new System.Drawing.Bitmap(1000,64));
        g.MeasureString("my text", myfont, mymaxstringwidth);

        Comment

        Working...