TextBox maximum number of characters (this is not MaxLength)

I use System.Windows.Forms.TextBox. According to the documents, the property MaxLengthcontrols the number of characters entered by the user, can be entered or inserted into the TextBox (that is, more than this can be added programmatically, using, for example, a function AppendTextor property Text). The current number of characters can be obtained from the property TextLength.

  • Is there a way to set the maximum number of characters without creating a custom limiter that triggers Clear()when the user limit is reached?
  • No matter what absolute maximum it can hold? Is it limited only by memory?
  • What happens when the maximum is reached / memory full? Failure? Are top x rows cleared?
  • What would be the best way to manually clear only the top lines? Substring work?

edit: I checked it to save more than 600 thousand characters, regardless MaxLength, after which I manually stopped the program and asked this question.

+5
source share
3 answers
  • Of course. Override / shadow AppendTextand Textin a derived class. See code below.
  • The support field for the property Textis a plain old string (private field System.Windows.Forms.Control::text). Thus, the maximum length is the maximum length of the string, which is "2 GB or about 1 billion characters" (see System.String ).
  • Why don't you try and see?
  • . Lines, , , , Text . , . ( , ) , cr/lfs. , , , , .

: 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;
        }
    }
}
+9

Text System.Windows.Forms.TextBox ,

+2

The theoretical limit is a string, ~ 2 GB. However, in reality, it depends on the conditions of your work process. It is equal to the size of the largest available contiguous memory partition that a line can allocate at any given time. I have a text box in the application with an error of about 450 MB.

0
source

All Articles