一个TextBox可显示字符的最大数量 [英] The maximum number of characters a TextBox can display

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

问题描述

刚才我看到一个问题:<一href=\"http://stackoverflow.com/questions/25708186/stringbuilder-wont-show-in-textbox-winforms-c\">StringBuilder不会显示在文本框(的WinForms,C#)。该帖子的作者无法显示自己的内容,这是大约50K字符的字符串,在他的单行文本框

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:

由于迭代10000次,生成的字符串是大,是没有得到显示在单行文本框。

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

所以我很好奇的最长单行文本框可以显示

我SO浏览,发现这个问题:<一href=\"http://stackoverflow.com/questions/10011508/textbox-maximum-amount-of-characters-its-not-maxlength\">TextBox最大字符数量(这不是最大长度),它清除了一些疑问,但不是全部。我还是想知道:

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. 由于文本属性是字符串键入,为什么它甚至不能处理50K字符时多行

  2. 有多少个字符文本框可容纳当?我们有没有办法让这个号码?

  3. 为什么属性会影响这种能力?

  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?

有关问题2第一部分,我做了下面的事情来验证:


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

我怀疑这个长度与分配给文本属性的内存。我做了一些研究线上,这<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.maxlength%28v=vs.110%29.aspx\"相对=nofollow> MSDN文档给了我一些启发:

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专业版,Windows 2000的服务器,Windows 2000高级服务器,Windows XP家庭版,Windows XP专业x64版,Windows Server 2003平台注:如果MaxLength属性设置为0时,字符,用户可以输入的最大数目是2147483646或量根据可用存储器,较小的一个。

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.

所以,我做了一个实验:我创建了2 文本框,即 textBox1的 TextBox2中 TextBox2中将显示 textBox1的的实时字符数。此外,我改变了最大长度属性 0 文本框。在code是这样的:

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();
}

原来,当文本的长度超过了 43679 文本完全消失了:

43680

因此​​,似乎分配给内存文字属性可以在43679字的我的电脑上举行。但我不知道这个数字对于所有计算机相同。我们有没有办法更深沉的方式来获得这个号码?

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?

在此先感谢!

推荐答案

从我的测试中我发现一个文本框不能的显示将超过32k的像素行给出的文本框的字体。

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

使用这个小试验平台。

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);
} 

您可以看到,显示一次消失的宽度将超过32K。对于选择大字体大小出现这种情况,只有约1350字。这应该从注释中解释我们的不同的结果,海事组织。

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.

更新:Acoording的答案在这篇文章这个限制是不是这么多文本框和台词,但有关Windows控件一般:

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:

汉斯帕桑特写道:

这是在Windows的体系结构限制。各种消息的
  显示位置在一个窗口,就像WM_MOUSEMOVE,报告的立场
  与16位的X和16位的一个32位的整数
  Y位置。因此,不能创建一个窗口,它比大
  short.MaxValue。

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.

所以,计算其显示的时候,文本框点击该限制,默默地/ gracfully(?)不显示任何东西。

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆