字符(这不是最大长度)文本框最高限额这不是、限额、文本框、长度

2023-09-04 00:33:50 作者:Amnesia 记忆缺失

我使用了一个 System.Windows.Forms.TextBox 。根据文档中,最大长度属性控制字符输入用户可以输入或粘贴到文本框(即超过可使用如编程方式添加量 AppendText 函数或文本属性)。字符的电流量可以从正文长度属性来了。

有什么办法来设置字符的最大数量,而不进行自定义限制器,它调用清除()当达到自定义限制? 不管,什么是绝对最大可以容纳?难道仅受内存? 当达到最大/内存已满,会发生什么?崩溃?顶的X线被清除? 什么是最好的方式来手动清除只有顶的X线?子字符串操作?

编辑:我已经测试了它持有超过60万字,无论最大长度的,在这一点上,我手动停止该程序,并提出了这个问题。

解决方案 确定。覆盖/阴影 AppendText 在派生类中的文本。见下面code。 为文本属性是一个普通的老字符串的支持字段(私有字段 System.Windows.Forms.Control的::文)。因此,最大长度是一个字符串,也就是2 GB,或约1十亿字符的最大长度(见的 System.String )。 你为什么不尝试一下,看看? 这取决于您的性能需求。您可以使用属性,但要注意,每次你把它整个文本将在内部解析成线。如果你正在推动内容长度的限制,这将是一个坏主意。这样更快的方法(在执行方面,而不是编码)将是压缩过的人物和计数的CR / LFS。当然,你需要确定你正在考虑一个行结束。

code:实施最大长度属性以编程方式设置文本即使在:

 使用系统;
使用System.Windows.Forms的;
命名空间WindowsFormsApplication5 {
    类TextBoxExt:文本框{
        新公共无效AppendText(字符串文本){
            如果(this.Text.Length == this.MaxLength){
                返回;
            }否则,如果(this.Text.Length + text.Length> this.MaxLength){
                base.AppendText(text.Substring(0,(this.MaxLength  -  this.Text.Length)));
            } 其他 {
                base.AppendText(文本);
            }
        }

        公共重写字符串文本{
            得到 {
                返回base.Text;
            }
            组 {
                如果(string.IsNullOrEmpty(值)及!&安培; value.Length> this.MaxLength){
                    base.Text = value.Substring(0,this.MaxLength);
                } 其他 {
                    base.Text =价值;
                }
            }
        }

        //还有:清除具有高性能顶的X线
        公共无效ClearTopLines(诠释计数){
            如果(计数< = 0){
                返回;
            }否则,如果(!this.Multiline){
                this.Clear();
                返回;
            }

            字符串的txt = this.Text;
            INT光标= 0,ixOf = 0,brkLength = 0,brkCount = 0;

            而(brkCount<计数){
                ixOf = txt.IndexOfBreak(光标,出brkLength);
                如果(ixOf℃,){
                    this.Clear();
                    返回;
                }
                光标= ixOf + brkLength;
                brkCount ++;
            }
            this.Text = txt.Substring(光标);
        }
    }

    公共静态类StringExt {
        公共静态INT IndexOfBreak(此字符串str,OUT INT长度){
            返回IndexOfBreak(海峡,0,出长度);
        }

        公共静态INT IndexOfBreak(此字符串str,诠释了startIndex,OUT INT长度){
            如果(string.IsNullOrEmpty(STR)){
                长度= 0;
                返回-1;
            }
            INT UB = str.Length  -  1;
            INT intchr;
            如果(在startIndex> UB){
                抛出新ArgumentOutOfRangeException();
            }
            对于(INT I =在startIndex; I< = UB;我++){
                intchr =海峡[I]
                如果(intchr == 0X0D){
                    如果(ⅰ&其中; UB&安培;&安培;海峡[I + 1] ==的0x0A){
                        长度= 2;
                    } 其他 {
                        长度= 1;
                    }
                    返回我;
                }否则,如果(intchr ==的0x0A){
                    长度= 1;
                    返回我;
                }
            }
            长度= 0;
            返回-1;
        }
    }
}
 

I'm using a System.Windows.Forms.TextBox. According to the docs, the MaxLength property 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 AppendText function or the Text property). The current amount of characters can be got from the TextLength property.

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? 随笔列表第2页 田想兵

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.

解决方案

Sure. Override / shadow AppendText and Text in a derived class. See code below. The backing field for the Text property is a plain old string (private field System.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 Lines property, but beware that every time you call it your entire text will 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.

Code: Enforce MaxLength property even when setting text programmatically:

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