Question

Look at this sample:

public partial class Form1 : Form
{
    private static string myString = null;

    private const int MAX_TEXT = 5460;

    public Form1()
    {
        InitializeComponent();

        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < MAX_TEXT; i++)
        {
            builder.Append('a');
        }

        myString = builder.ToString();

        this.Paint += Form1_Paint;
    }

    void Form1_Paint(object sender, PaintEventArgs e)
    {
        TextRenderer.DrawText(
            e.Graphics,
            myString,
            this.Font,
            new Point(10, 30),
            Color.Black);
    }
}

When I set the MAX_TEXT to 5461, the string is not drawn. Do you know if the native mechanism has a limit to draw text, if/or I can setup the options to get it working?

Was it helpful?

Solution

I think you hit the limitation of the TextRenderer class, which I think is calling the DrawTextEx API function under the hood. If you try to put your builder.ToString() results into a TextBox, it won't show up either.

If for some reason you need to print a string that long, you would have to revert back to the DrawString method:

e.Graphics.DrawString(myString, this.Font, Brushes.Black, new Point(10, 30));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top