The maximum number of characters a TextBox can display

From my tests I find that a Textbox can’t display lines that would exceed 32k pixels given the Font of the TextBox.

Using this little testbed

public Form1()
{
    InitializeComponent();

    textBox1.Font = new System.Drawing.Font("Consolas", 32f); 
    G = textBox1.CreateGraphics();
    for (int i = 0; i < 100; i++) textBox1.Text += i.ToString("0123456789");
}

Graphics G;

private void button2_Click(object sender, EventArgs e)
{   
   for (int i = 0; i < 10; i++) textBox1.Text += i.ToString("x");
   Console.WriteLine( textBox1.Text.Length.ToString("#0   ") 
       + G.MeasureString(textBox1.Text, textBox1.Font).Width);
} 

You can see that the display vanishes once the width would exceed 32k. For the chosen big Fontsize this happens with only about 1350 characters. This should explain our different results from the comments, imo.

The Text still holds the full length of the data.

Update: Acoording to the answers in this post this limit is not so much about TextBoxes and their Lines but about Windows Controls in general:

Hans Passant writes:

This is an architectural limitation in Windows. Various messages that
indicate positions in a window, like WM_MOUSEMOVE, report the position
in a 32-bit integer with 16-bits for the X and 16-bits for the
Y-position. You therefore cannot create a window that’s larger than
short.MaxValue.

So when calculating its display, the TextBox hits that limit and silently/gracfully(??) doesn’t display anything at all.

Leave a Comment