本文介绍了TextBox 可以显示的最大字符数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚才看到一个问题: 这个限制不是关于文本框和它们的行,而是关于 Windows 控件:

汉斯·帕桑特写道:

这是 Windows 中的架构限制.各种消息在窗口中指示位置,如 WM_MOUSEMOVE,报告位置在一个 32 位整数中,X 为 16 位,X 为 16 位Y 位置.因此,您不能创建大于short.MaxValue.

因此,在计算其显示时,TextBox 达到了该限制并且静默/优雅地(??) 根本不显示任何内容.

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:

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:

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:

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?

解决方案

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:

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

这篇关于TextBox 可以显示的最大字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 05:37