The maximum number of characters that a TextBox can display

Now I saw a problem: StringBuilder will not display in TextBox (WinForms, C #) . The author of the message could not display its contents, which is a string of about 50 thousand characters in a single line of a TextBox .

The response indicated that it should change the MultiLine property to true . The explanation is given in the comment:

Since iterating 10,000 times, the generated line is large and does not appear in the text field of one line.

So, I'm curious that the length max can be displayed in a text field with one line .

I looked through SO and found this question: The maximum number of TextBox characters (this is not MaxLength) , it eliminates some doubts, but not all. I still want to know:

  • Since the Text property is of type String , why can't it even handle 50k characters if MultiLine is false ?
  • How many characters a TextBox can be held if MultiLine false ? Do we have a way to get this number?
  • Why does the MultiLine property affect this feature?

For the first part of question 2, I checked the following things:

I suspected this length was due to memory allocated for the Text property. I did some research on the Internet and this MSDN Documentation gave me some ideas:

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 a user can enter is 2147483646 or the amount based on available memory, whichever is less.

So, I did an experiment: I created 2 TextBox , namely textBox1 and textBox2 . textBox2 displays the number of real-time textBox1 . In addition, I changed the MaxLength property to 0 for the TextBox . The code is as follows:

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

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

4367943680

Thus, the memory allocated for the Text property may contain 43679 characters on my computer . But I'm not sure that this number is the same for all computers. Do we have a more sophisticated way to get this number?

Thanks in advance!

+5
source share
1 answer

From my tests, I found that a text field cannot display lines that exceed 32k pixels, given the TextBox font.

Using this small test bench

 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 disappears as soon as the width exceeds 32k. For the selected large font, this only happens with 1350 characters. This should explain our different results from the comments, imo.

The text still contains the entire length of the data.

Update : according to the answers in this post, this limit is not so much about text blocks and their strings, but about Windows Controls in general

Hans Passant writes:

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

Thus, when calculating its display, the TextBox falls into this limit and silently / correctly (??) does not display anything.

+10
source

Source: https://habr.com/ru/post/1202003/


All Articles