TextBox maximum amount of characters (it's not MaxLength)
我正在使用
edit:我已经测试过它可以容纳超过60万个字符,而无论
代码:即使以编程方式设置文本,也强制执行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | 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; } } } |
理论上的限制是大约2GB的字符串。但是,实际上,这取决于运行过程中的条件。它等于字符串在任何给定时间可以分配的最大可用连续内存部分的大小。我的应用程序中有一个文本框,错误大约为450MB。