Gets or sets the maximum number of characters the user can type or paste into the rich text box control.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
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.
The following code example demonstrates how to use the property to determine if text being assigned to a RichTextBox control is larger than the value assigned to the 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 Sub AddMyText(ByVal textToAdd As String) ' Determine if the text to add is larger than the max length property. If textToAdd.Length > richTextBox1.MaxLength Then ' Alert user text is too large. MessageBox.Show("The text is too large to addo to the RichTextBox") ' Add the text to be added to the control. Else richTextBox1.SelectedText = textToAdd End If End Sub