C# TextBox 最大字符数(不是 MaxLength)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10011508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TextBox maximum amount of characters (it's not MaxLength)
提问by David S.
I'm using a System.Windows.Forms.TextBox. According to the docs, the MaxLengthproperty controls the amount of characters enter a user can type or paste into the TextBox (i.e. more than that can be added programmatically by using e.g. the AppendTextfunction or the Textproperty). The current amount of characters can be got from the TextLengthproperty.
我正在使用一个System.Windows.Forms.TextBox. 根据文档,该MaxLength属性控制用户可以输入或粘贴到 TextBox 中的字符数量(即,可以使用AppendText函数或Text属性等以编程方式添加更多字符)。可以从TextLength属性中获取当前字符数。
- Is there any way to set the maximum amount of characters without making a custom limiter which calls
Clear()when the custom limit is reached? - Regardless, what is the absolute maximum it can hold? Is it only limited by memory?
- What happens when the maximum is reached / memory is full? Crash? Top x lines is cleared?
- What would be the best way to manually clear only the top x lines? Substring operation?
- 有什么方法可以设置最大字符数而不使用
Clear()在达到自定义限制时调用的自定义限制器? - 无论如何,它可以容纳的绝对最大值是多少?是不是只受内存限制?
- 达到最大值/内存已满时会发生什么?碰撞?顶部 x 行被清除?
- 仅手动清除顶部 x 行的最佳方法是什么?子串操作?
edit: I have tested it to hold more than 600k characters, regardless of MaxLength, at which point I manually stopped the program and asked this question.
编辑:我已经测试过它可以容纳超过 600k 个字符,无论MaxLength,此时我手动停止了程序并提出了这个问题。
采纳答案by Joshua Honig
- Sure. Override / shadow
AppendTextandTextin a derived class. See code below. - The backing field for the
Textproperty is a plain old string (private fieldSystem.Windows.Forms.Control::text). So the maximum length is the max length of a string, which is "2 GB, or about 1 billion characters" (see System.String). - Why don't you try it and see?
- It depends on your performance requirements. You could use the
Linesproperty, but beware that every time you call it your entiretextwill be internally parsed into lines. If you're pushing the limits of content length this would be a bad idea. So that faster way (in terms of execution, not coding) would be to zip through the characters and count the cr / lfs. You of course need to decide what you are considering a line ending.
- 当然。覆盖/阴影
AppendText并Text在派生类中。请参阅下面的代码。 - 该
Text属性的支持字段是一个普通的旧字符串(私有字段System.Windows.Forms.Control::text)。因此最大长度是字符串的最大长度,即“2 GB,或大约 10 亿个字符”(请参阅System.String)。 - 你为什么不试试看呢?
- 这取决于您的性能要求。您可以使用该
Lines属性,但请注意,每次调用它时,您的整个内容text都会在内部被解析为行。如果您要突破内容长度的限制,这将是一个坏主意。因此,更快的方法(在执行方面,而不是编码方面)是压缩字符并计算 cr / lfs。您当然需要决定您正在考虑的行尾。
Code: Enforce MaxLengthproperty even when setting text programmatically:
代码:MaxLength即使在以编程方式设置文本时也强制执行属性:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication5 {
class TextBoxExt : TextBox {
new public void AppendText(string text) {
if (this.Text.Length == this.MaxLength) {
return;
} else if (this.Text.Length + text.Length > this.MaxLength) {
base.AppendText(text.Substring(0, (this.MaxLength - this.Text.Length)));
} else {
base.AppendText(text);
}
}
public override string Text {
get {
return base.Text;
}
set {
if (!string.IsNullOrEmpty(value) && value.Length > this.MaxLength) {
base.Text = value.Substring(0, this.MaxLength);
} else {
base.Text = value;
}
}
}
// Also: Clearing top X lines with high performance
public void ClearTopLines(int count) {
if (count <= 0) {
return;
} else if (!this.Multiline) {
this.Clear();
return;
}
string txt = this.Text;
int cursor = 0, ixOf = 0, brkLength = 0, brkCount = 0;
while (brkCount < count) {
ixOf = txt.IndexOfBreak(cursor, out brkLength);
if (ixOf < 0) {
this.Clear();
return;
}
cursor = ixOf + brkLength;
brkCount++;
}
this.Text = txt.Substring(cursor);
}
}
public static class StringExt {
public static int IndexOfBreak(this string str, out int length) {
return IndexOfBreak(str, 0, out length);
}
public static int IndexOfBreak(this string str, int startIndex, out int length) {
if (string.IsNullOrEmpty(str)) {
length = 0;
return -1;
}
int ub = str.Length - 1;
int intchr;
if (startIndex > ub) {
throw new ArgumentOutOfRangeException();
}
for (int i = startIndex; i <= ub; i++) {
intchr = str[i];
if (intchr == 0x0D) {
if (i < ub && str[i + 1] == 0x0A) {
length = 2;
} else {
length = 1;
}
return i;
} else if (intchr == 0x0A) {
length = 1;
return i;
}
}
length = 0;
return -1;
}
}
}
回答by curtisk
The Text property of System.Windows.Forms.TextBoxis a string, so in theory it can be the max length of a string
回答by S. Steff
The theoretical limit is that of a string, ~2GB. However, in reality, it depends upon the conditions in your running process. It equates to the size of the largest available contiguous section of memory that a string can allocate at any given time. I have a textbox in an application that is erroring at about 450MB.
理论上的限制是一个字符串,〜2GB。但是,实际上,这取决于您运行过程中的条件。它等于字符串在任何给定时间可以分配的最大可用连续内存部分的大小。我在应用程序中有一个文本框,错误大约为 450MB。

