C# 文本框最大字符数(它不是MaxLength)
我使用的是C# 文本框最大字符数(它不是MaxLength),c#,.net,winforms,C#,.net,Winforms,我使用的是系统.Windows.Forms.TextBox。根据文档,MaxLength属性控制用户可以输入或粘贴到文本框中的字符数(即,可以通过使用AppendText函数或Text属性以编程方式添加的字符数)。可以从TextLength属性获取当前字符数 是否有任何方法可以设置最大字符数,而无需在达到自定义限制时调用Clear() 不管怎样,它能保持的绝对最大值是多少?它仅仅是受记忆的限制吗 当达到最大值/内存已满时会发生什么情况?崩溃顶部的x线已清除 手动清除顶部x行的最佳方法是什么?子串
系统.Windows.Forms.TextBox
。根据文档,MaxLength
属性控制用户可以输入或粘贴到文本框中的字符数(即,可以通过使用AppendText
函数或Text
属性以编程方式添加的字符数)。可以从TextLength
属性获取当前字符数
Clear()
编辑:我已经测试过它可以容纳超过600k个字符,不管它的最大长度是多少,这时我手动停止了程序并问了这个问题。System.Windows.Forms.TextBox的Text属性是一个字符串,所以理论上它可以是
AppendText
和Text
。请参阅下面的代码Text
属性的支持字段是一个普通的旧字符串(private fieldSystem.Windows.Forms.Control::Text
)。因此,最大长度是字符串的最大长度,即“2GB,或大约10亿个字符”(请参阅)Lines
属性,但请注意,每次调用它时,您的整个文本将在内部解析为行。如果你要突破内容长度的限制,这将是一个坏主意。因此,更快的方法(就执行而言,而不是编码而言)是压缩字符并计算cr/LF。当然,你需要决定一行的结尾是什么李>
代码:即使以编程方式设置文本,也强制执行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;
}
}
}
使用系统;
使用System.Windows.Forms;
命名空间Windows窗体应用程序5{
类TextBoxExt:TextBox{
新的公共文本(字符串文本){
if(this.Text.Length==this.MaxLength){
返回;
}else if(this.Text.Length+Text.Length>this.MaxLength){
base.AppendText(text.Substring(0,(this.MaxLength-this.text.Length));
}否则{
base.AppendText(文本);
}
}
公共重写字符串文本{
得到{
返回base.Text;
}
设置{
如果(!string.IsNullOrEmpty(value)&&value.Length>this.MaxLength){
base.Text=value.Substring(0,this.MaxLength);
}否则{
base.Text=值;
}
}
}
//另外:以高性能清除顶部X行
公共无效ClearTopLines(整数计数){
如果(计算ub){
抛出新ArgumentOutOfRangeException();
}
对于(int i=startIndex;i理论上的限制是字符串的限制,~2GB。然而,实际上,它取决于运行进程中的条件。它等于字符串在任何给定时间可以分配的最大可用连续内存部分的大小。我在应用程序中有一个文本框,其错误大约为450MB。hav你试过什么吗?@satish我试过4),textBox.Text=textBox.Text.Remove(0,charsToRemove)似乎很好,但当charsToRemove较大时(用100k试过)会导致一个小挂起。这不是什么大问题。其他我不能尝试,因为我还没有达到任何最大限制。RichTextBox对你来说会是更好的选择吗?如是说:“已经使用TextBox控件的应用程序可以很容易地调整为使用RichTextBox控件。但是,RichTextBox控件没有与TextBox控件相同的64K字符容量限制。“我是来问第三个问题的,我发现这个答案非常有用--…不是。至少你可以说你真的不知道。@JorgeGalvão我的观点是,测试这个问题很简单:制作一个文本框,将其值设置为新字符串('',int.MaxValue)
或类似的,然后看看会发生什么。StackOverflow社区鼓励海报先尝试,然后寻求帮助。就好像您将MaxLength
属性设置为0一样,它将使用可能的最高限制。嗯,可能的最高限制似乎是2147483647-对于文本框而言,至少:)