9

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.

  1. 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?
  2. Regardless, what is the absolute maximum it can hold? Is it only limited by memory?
  3. What happens when the maximum is reached / memory is full? Crash? Top x lines is cleared?
  4. What would be the best way to manually clear only the top x lines? Substring operation?

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.

David S.
  • 5,965
  • 2
  • 40
  • 77
  • have you tried anything? – Virus Apr 04 '12 at 13:10
  • @satish I have tried 4), textBox.Text = textBox.Text.Remove(0, charsToRemove) seems to work fine, but it causes a small hang when charsToRemove is large (tried with 100k). This is not a big deal. The others I can't try because I have yet to reach any maximum limit. – David S. Apr 04 '12 at 13:37
  • Would a RichTextBox be a better choice for you? [MSDN](http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.aspx) says this: "Applications that already use TextBox controls can easily be adapted to make use of RichTextBox controls. However, the RichTextBox control does not have the same 64K character capacity limit of the TextBox control." – Brad Rem Apr 04 '12 at 14:41

3 Answers3

12
  1. Sure. Override / shadow AppendText and Text in a derived class. See code below.
  2. 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).
  3. Why don't you try it and see?
  4. 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;
        }
    }
}
Joshua Honig
  • 12,925
  • 8
  • 53
  • 75
  • I came here for the 3rd question and I've found this answer very useful -_- ... not. At least you could say you don't really know. – Jorge Galvão Dec 21 '13 at 19:14
  • 5
    @JorgeGalvão My point is that testing this is trivial: Make a text box, set its value to `new string(' ', int.MaxValue)` or something similar, and see what happens. The StackOverflow community encourages posters to try first, then ask for help. – Joshua Honig Dec 22 '13 at 01:44
3

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.

S. Steff
  • 46
  • 2
2

The Text property of System.Windows.Forms.TextBox is a string, so in theory it can be the max length of a string

Community
  • 1
  • 1
curtisk
  • 19,950
  • 4
  • 55
  • 71
  • 2
    As [pointed out in this answer](http://stackoverflow.com/a/13528057/1037948) if you set the `MaxLength` property to 0 it will use the highest possible limit. – drzaus Jan 16 '15 at 14:18
  • 1
    Well, the **highest possible limit** seems to be **2147483647** - For a `TextBox`, at least :) – Momoro Feb 11 '20 at 05:06