9

Just now I saw a problem: StringBuilder Won't Show In TextBox (WinForms, C#). The author of the post could not display his content, which is a string of around 50k characters, in his single-line TextBox.

The answer pointed out that he should change the MultiLine property to true. An explanation gave in the comment stated:

Since the iteration is 10000 times, the string generated is large and is not getting displayed in a single line textbox.

So I'm curious about the max length a single line text box can display.

I browsed SO and found this question: TextBox maximum amount of characters (it's not MaxLength), it clears some doubt, but not all. I still want to know:

  1. Since Text property are of String type, why it could not even handle 50k characters when MultiLine is false?
  2. How many characters a TextBox can hold when MultiLine is false? Do we have a way to get this number?
  3. Why MultiLine property affects this capability?

For question 2 first part, I did the following things to verify:

I suspected this length was related to the memory allocated to Text property. I did some research online, and this MSDN Documentation gave me some insights:

Windows NT 4.0, Windows 2000, Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, Windows XP Home Edition, Windows XP Professional x64 Edition, Windows Server 2003 Platform Note: If the MaxLength property is set to 0, the maximum number of characters the user can enter is 2147483646 or an amount based on available memory, whichever is smaller.

So I did an experiment: I created 2 TextBox, namely textBox1 and textBox2. textBox2 will display the real time character count of textBox1. In addition, I changed the MaxLength property to 0 for both TextBox. The code looks like this:

public Form1()
{
    InitializeComponent();

    textBox1.TextChanged += (s, e) => textBox2.Text = textBox1.Text.Length.ToString();
}

It turned out that when the length of text exceeds 43679, the Text completely gone:

43679 43680

So it seems the memory allocated to Text property can hold upon 43679 characters on my computer. But I'm not sure if this number is the same for all computers. Do we have a way more sophisticate way to get this number?

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
nevets
  • 4,631
  • 24
  • 40
  • I couldn't reproduce the problem in the post you refer to. Can you? If so what is your setup? TextBoxes can hold __much__ more text. I see that you are using single line TextBoxes. There may well ba a limit to the Length of a visible single Line.. – TaW Sep 07 '14 at 10:11
  • I can reproduce. I think it because of the second question I mentioned: your memory allocated to `Text` property of single-line TextBox is over 50K, but mine only 43K. – nevets Sep 07 '14 at 10:13
  • I can reproduce that the TextBox will not __display__ a line longer than about 6120 charcters. It can __hold__ any Length, but it won't display such a long line. – TaW Sep 07 '14 at 10:20
  • Hmm for me it can **display** up to 43K characters, like the pic I posted in my question. – nevets Sep 07 '14 at 10:23
  • I see some weird differences in my test depending on the way I add the text. But it is always there, just not visible.. Maybe the Font is the reason..? - Update: Yes it __is__ the font! Looks like the resulting Width can't exceed 32k. – TaW Sep 07 '14 at 10:26
  • Oh interesting observation @TaW. Can you explain more? – nevets Sep 07 '14 at 10:31
  • Why did you accept the answer? You clearly said that your textbox displays up to 43679 characters but the answer tries to convince you that the limit is related to pixels. For me, it clearly isn't. And it must not have been true for you in 2014 already, since each of your 43679 characters is wider than 1 pixel. – Thomas Weller Dec 26 '21 at 18:03

1 Answers1

13

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.

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111
  • this is really an interesting and sharp observation I would say. I guess the 32K would be 32767 pixels? Can you find any official backup for your observation? Thanks :D – nevets Sep 07 '14 at 10:44
  • 1
    Yes. If [Hans Passant's word](http://stackoverflow.com/questions/8064678/windows-forms-panel-32767-size-limit) is good engh for us.. Looks like this is not so much about TextBoxes and Lines but about Controls and their dimensions and about old win32 API pointer sizes. – TaW Sep 07 '14 at 10:53
  • Well I take his words as golden rules lol. Many thanks! Can you edit the post to include his quote? I think it will be better for followers :) – nevets Sep 07 '14 at 10:55
  • The code is kinda strange. What do you expect `97.ToString("0123456789")` to return? If you always want to add 10 characters, use `+= "0123456789"`. Similar for `i.ToString("x")` where 0<=x<=9. – Thomas Weller Dec 26 '21 at 17:57
  • As of today, this answer is no longer true. The text renders happily at 3660 characters and 88464 pixels width. It will still render at 43679 characters and stop at 43680 characters and it's independent of the font. I have Windows 10 21H1. – Thomas Weller Dec 26 '21 at 17:59
  • Actually, OP mentioned in 2014 already that his textbox displays up to 43679 characters and each of his 43679 characters is clearly wider than 1 pixel. – Thomas Weller Dec 26 '21 at 18:05