The Maximum Number of Characters a Textbox Can Display

The maximum number of characters a TextBox can display

From my tests I find that a Textbox can't display lines that would exceed 32k pixels given the Font of the TextBox.

Using this little testbed

public Form1()
{
InitializeComponent();

textBox1.Font = new System.Drawing.Font("Consolas", 32f);
G = textBox1.CreateGraphics();
for (int i = 0; i < 100; i++) textBox1.Text += i.ToString("0123456789");
}

Graphics G;

private void button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++) textBox1.Text += i.ToString("x");
Console.WriteLine( textBox1.Text.Length.ToString("#0 ")
+ G.MeasureString(textBox1.Text, textBox1.Font).Width);
}

You can see that the display vanishes once the width would exceed 32k. For the chosen big Fontsize this happens with only about 1350 characters. This should explain our different results from the comments, imo.

The Text still holds the full length of the data.

Update: Acoording to the answers in this post this limit is not so much about TextBoxes and their Lines but about Windows Controls in general:

Hans Passant writes:

This is an architectural limitation in Windows. Various messages that
indicate positions in a window, like WM_MOUSEMOVE, report the position
in a 32-bit integer with 16-bits for the X and 16-bits for the
Y-position. You therefore cannot create a window that's larger than
short.MaxValue.

So when calculating its display, the TextBox hits that limit and silently/gracfully(??) doesn't display anything at all.

Restrict Maximum Number of Characters in a Textbox

The problem here was that I was limiting the size of the string being displayed on my form after I pull the data from the database when I used myTextBox.MaxLength = 250 when loading my form. here I was actually limiting the amount of text being displayed and not the amount of text the user could add.

by adding the code myTextBox.MaxLength = 250 when loading the component it now limits the amount of character I can type in the text box.

TextBox maximum amount of characters (it's not MaxLength)

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

How to set maximum number of characters in number type textbox?

There are a few of these "how do I further control type="number" fields" questions here on SO. It boils down to:

  1. You make it a type="number" and accept that you can't further mask the input vs. what the browser does to implement a numeric input, or

  2. You make it type="text" and use pattern and/or maxlength to enforce the specific pattern you want, and accept that the browser won't add a specific UI for numbers.

CEdit control maximum length? (in characters it can display)

I found the documentation is wrong when mentioning the default size for a single line CEdit control in vista.

I ran this code:

CWnd* pWnd = dlg.GetDlgItem(nItemId);
CEdit *edit = static_cast<CEdit*>(pWnd); //dynamic_cast does not work
if(edit != 0)
{
UINT limit = edit->GetLimitText(); //The current text limit, in bytes, for this CEdit object.
//value returned: 30000 (0x7530)
edit->SetLimitText(0);
limit = edit->GetLimitText();
//value returned: 2147483646 (0x7FFFFFFE)
}

the documentation states:

Before EM_SETLIMITTEXT is called, the
default limit for the amount of text a
user can enter in an edit control is
32,767 characters.

which is apparently wrong.

limiting the text box to have only particular limit of numbers and characters

Below code will help you.
Maximum and Minimum character length validation for 5 character.

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator Display = "Dynamic" ControlToValidate = "TextBox1" ID="RegularExpressionValidator1" ValidationExpression = "^[\s\S]{0,5}$" runat="server" ErrorMessage="Your maximum limit is only 5"></asp:RegularExpressionValidator>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator Display = "Dynamic" ControlToValidate = "TextBox1" ID="RegularExpressionValidator1" ValidationExpression = "^[\s\S]{5,}$" runat="server" ErrorMessage="Minimum required limit is 5"></asp:RegularExpressionValidator>

What is the maximum amount of characters for an ASP.NET textbox with MultiLine TextMode enabled?

There is no upper limit to the number of characters in a WebControls.Textbox in multiline mode. The only limits are total post data from a form and that depends on your web-servers set-up (I presume IIS) which I think is about 2mb by default in IIS but don't quote me on that.

What is happening when you try to type beyond 1000 charatcters?

c# - Is there a character limit on textboxes?

Win32 documentation says:

Before EM_SETLIMITTEXT is called, the default limit for the amount of text a user can enter in an edit control is 32,767 characters.

For single-line edit controls, the text limit is [...] 0x7FFFFFFE (2147483646) bytes

TextBoxBase.MaxLength documentation confirms this:

The number of characters that can be entered into the control. The default is 32767.

If the MaxLength property is set to 0, the maximum number of characters the user can enter is 2147483646 [or 32,766 on Windows ME,] or an amount based on available memory, whichever is smaller.

Limit number of characters allowed in form input text field

maxlength:

The maximum number of characters that will be accepted as input. This can be greater that specified by SIZE , in which case the field
will scroll appropriately. The default is unlimited.

<input type="text" maxlength="2" id="sessionNo" name="sessionNum" onkeypress="return isNumberKey(event)" />

However, this may or may not be affected by your handler. You may need to use or add another handler function to test for length, as well.



Related Topics



Leave a reply



Submit