29

I have a string like this:

string s = "This is my string";

I am creating a Telerik report and I need to define a textbox that is the width of my string. However the size property needs to be set to a Unit (Pixel, Point, Inch, etc). How can I convert my string length into, say a Pixel so I can set the width?

EDIT: I have tried getting a reference to the graphics object, but this is done in a class that inherits from Telerik.Reporting.Report.

ScottG
  • 10,711
  • 25
  • 82
  • 111

6 Answers6

72

Without using of a control or form:

using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(new Bitmap(1, 1)))
{
    SizeF size = graphics.MeasureString("Hello there", new Font("Segoe UI", 11, FontStyle.Regular, GraphicsUnit.Point));
}

Or in VB.Net:

Using graphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(New Bitmap(1, 1))
    Dim size As SizeF = graphics.MeasureString("Hello there", New Font("Segoe UI", 11, FontStyle.Regular, GraphicsUnit.Point))
End Using
shA.t
  • 16,580
  • 5
  • 54
  • 111
Tom Anderson
  • 10,807
  • 3
  • 46
  • 63
27
Size textSize = TextRenderer.MeasureText("How long am I?", font);
Wachburn
  • 2,842
  • 5
  • 36
  • 59
13

In this case, I usually use a dirty, but simple way:

  • I add an invisible Label that its AutoSize property is true -dirty work-.
  • When I want to have the Width for a specific string, I set it to the Label.Text.
  • The Width of the Label will give me the correct value.
shA.t
  • 16,580
  • 5
  • 54
  • 111
4

You can create an instance of a graphics object an use the MeasureString() method. But you will need to pass it the font name, font size, and other info.

shA.t
  • 16,580
  • 5
  • 54
  • 111
Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173
  • I have font name, size, etc. I have looked at the MeasureString method, but cannot figure out how to create a Graphics object reference. I don't have a form or control, I am just trying to figure this out in a class. – ScottG Jan 16 '09 at 20:44
4

Depends on the font, too. String length isn't sufficient.

duffymo
  • 305,152
  • 44
  • 369
  • 561
1

I wrote three identical functions as shown below. Only the signatures are changed to enable all functions to live in harmony with the same name. I placed them in a Class called "StringExtensions" and declared each function as "Shared" so external coding could access them without creating an instance variable of the class.

  Public Shared Function TextLengthInPixels(oTextBox As TextBox, bTrim As Boolean) As Integer
      Return TextRenderer.MeasureText(IIf(bTrim, oTextBox.Text.Trim, oTextBox.Text), oTextBox.Font).Width
  End Function

  Public Shared Function TextLengthInPixels(oLabel As Label, bTrim As Boolean) As Integer
      Return TextRenderer.MeasureText(IIf(bTrim, oLabel.Text.Trim, oLabel.Text), oLabel.Font).Width
  End Function

  Public Shared Function TextLengthInPixels(oRTB As RichTextBox, bTrim As Boolean) As Integer
      Return TextRenderer.MeasureText(IIf(bTrim, oRTB.Text.Trim, oRTB.Text), oRTB.Font).Width
  End Function

Just use whichever one you want (or write more for other controls which have a "text" property).

Chris Raisin
  • 384
  • 3
  • 7