3

I have a long string (~100k characters). I need to know the length for this string. I call

Size s = TextRenderer.MeasureText(graphics, text, font);

But it returns width equals 7. Only if text length <= 43679 it returns correct value!

Also, if I insert the text in the text box, the text is not visible in the text box! I can select text with mouse, copy via "Ctrl+C", but text not visible. MaxLength property is more than the text length.

I've looked on msdn, but haven't found any information about maximum text length which is used in MeasureText and TextBox.

Where I can find documentation about this? Is there any way to increase maximum text length? Do these values depend on the operating system and computer performance?

Nikolay
  • 587
  • 6
  • 20
  • I've heard of this problem before, it is a Windows bug. The kind that do exist when you do insane things like this. The workaround is pretty obvious, if the string contains more than a thousand characters then you just don't care anymore how wide the string might be, it is never going to fit. – Hans Passant Oct 08 '14 at 09:04
  • Related: https://stackoverflow.com/questions/25709069/the-maximum-number-of-characters-a-textbox-can-display – Thomas Weller Dec 26 '21 at 19:43
  • Related_ https://stackoverflow.com/questions/14355427/is-43679-a-magic-number – Thomas Weller Dec 26 '21 at 19:43
  • Related: https://stackoverflow.com/questions/30867395/cant-insert-more-than-43679-character-to-sql-server-single-cell – Thomas Weller Dec 26 '21 at 19:43

2 Answers2

0

Try using this code for measuring strings, textrenderer isnt accurate

protected int _MeasureDisplayStringWidth ( Graphics graphics, string text, Font font )
{
    if ( text == "" )
        return 0;

    StringFormat format = new StringFormat ( StringFormat.GenericDefault );
    RectangleF rect = new RectangleF ( 0, 0, 1000, 1000 );
    CharacterRange[] ranges = { new CharacterRange ( 0, text.Length ) };
    Region[] regions = new Region[1];

    format.SetMeasurableCharacterRanges ( ranges );
    format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;

    regions = graphics.MeasureCharacterRanges ( text, font, rect, format );
    rect = regions[0].GetBounds ( graphics );

    return (int)( rect.Right );
}

credit to problem with TextRenderer.MeasureText

Other then that also try using stringbuilder with long strings and then even split the strings into smaller ones and measure those and add it together

Community
  • 1
  • 1
Vajura
  • 1,112
  • 7
  • 16
  • It does not work. graphics.MeasureCharacterRanges(text, font, rect, format) throw "A generic error occurred in GDI+." exception with long text length (ex. 100k characters). – Nikolay Oct 08 '14 at 10:20
  • my tests: the maximum length for MeasureCharacterRanges is only 32 chars. – Ishma Aug 23 '18 at 18:35
0

Similar example was given Here . Also they addressed the maxLength issue on msdn

Important : See how the MaxLength is used in the article.

private static void DrawALineOfText(PaintEventArgs e)
{
    // Declare strings to render on the form. 
    string[] stringsToPaint = { "Tail", "Spin", " Toys" };

    // Declare fonts for rendering the strings.
    Font[] fonts = { new Font("Arial", 14, FontStyle.Regular), 
        new Font("Arial", 14, FontStyle.Italic), 
        new Font("Arial", 14, FontStyle.Regular) };

    Point startPoint = new Point(10, 10);

    // Set TextFormatFlags to no padding so strings are drawn together.
    TextFormatFlags flags = TextFormatFlags.NoPadding;

    // Declare a proposed size with dimensions set to the maximum integer value.
    Size proposedSize = new Size(int.MaxValue, int.MaxValue);

    // Measure each string with its font and NoPadding value and  
    // draw it to the form. 
    for (int i = 0; i < stringsToPaint.Length; i++)
    {
        Size size = TextRenderer.MeasureText(e.Graphics, stringsToPaint[i], 
            fonts[i], proposedSize, flags);
        Rectangle rect = new Rectangle(startPoint, size);
        TextRenderer.DrawText(e.Graphics, stringsToPaint[i], fonts[i],
            startPoint, Color.Black, flags);
        startPoint.X += size.Width;
    }

}

Source

Nuru Salihu
  • 4,756
  • 17
  • 65
  • 116
  • I saw this code. It does not explain why TextRenderer.MeasureText() do not work when the text is long. – Nikolay Oct 08 '14 at 10:27