Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

RichTextBox.MaxLength Property

Gets or sets the maximum number of characters the user can type or paste into the rich text box control.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public override int MaxLength { get; set; }
/** @property */
public int get_MaxLength ()

/** @property */
public void set_MaxLength (int value)

public override function get MaxLength () : int

public override function set MaxLength (value : int)

Property Value

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

Exception typeCondition

ArgumentException

The value assigned to the property is less than 0.

When this property is set to 0, the maximum length of the text that can be entered in the control is 64 KB of characters. This property is typically used when the RichTextBox is used to display a single line of rich text format (RTF) text. You can use this property to restrict the length of text entered in the control for values such as postal codes and telephone numbers, or to restrict the length of text entered when the data is to be entered in a database. You can limit the text entered into the control to the maximum length of the corresponding field in the database.

NoteNote

In code, you can set the value of the Text property to a value that has a length greater than the value specified by the MaxLength property. This property only affects text entered into the control at run time.

The following code example demonstrates how to use the MaxLength property to determine if text being assigned to a RichTextBox control is larger than the value assigned to the MaxLength property. If the text is not larger, the example uses the SelectedText property to assign the text to the control. This example requires that a RichTextBox control, named richTextBox1, has been added to a form and that the method in the example is called with text supplied to the parameter that is to be pasted into the control. The example also requires that the MaxLength property has been set to a value to limit text entry into the RichTextBox.

private void AddMyText(string textToAdd)
{
    // Determine if the text to add is larger than the max length property.
    if (textToAdd.Length > richTextBox1.MaxLength)
        // Alert user text is too large.
        MessageBox.Show("The text is too large to addo to the RichTextBox");
    else
        // Add the text to be added to the control.
        richTextBox1.SelectedText = textToAdd;
}

private void AddMyText(String textToAdd)
{
    // Determine if the text to add is larger than the max length property.
    if (textToAdd.get_Length() > richTextBox1.get_MaxLength()) {
        // Alert user text is too large.
        MessageBox.Show("The text is too large to addo to the RichTextBox");
    } 
    else {
        // Add the text to be added to the control.
        richTextBox1.set_SelectedText(textToAdd);
    }
} //AddMyText 

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2015 Microsoft