RichTextBox.MaxLength 属性

定义

获取或设置用户可在多格式文本框控件中键入或粘贴的最大字符数。

public:
 virtual property int MaxLength { int get(); void set(int value); };
public override int MaxLength { get; set; }
member this.MaxLength : int with get, set
Public Overrides Property MaxLength As Integer

属性值

可以在文本框控件中输入的字符数。 默认值为 Int32.MaxValue

例外

分配给属性的值小于零。

示例

下面的代码示例演示如何使用 MaxLength 属性来确定分配给 RichTextBox 控件的文本是否大于分配给 属性 MaxLength 的值。 如果文本不较大,则示例使用 SelectedText 属性将文本分配给 控件。 此示例要求 RichTextBox 已将名为 richTextBox1的控件添加到窗体中,并且调用示例中的方法时会提供要粘贴到控件中的 参数的文本。 该示例还要求 已MaxLength将 属性设置为 一个值,以限制文本输入到 。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 add to the RichTextBox" ); // Add the text to be added to the control.
      else
         richTextBox1->SelectedText = textToAdd;
   }
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 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

注解

当此属性设置为 0 时,可在控件中输入的文本的最大长度为 64 KB。 当 使用 (RTF) 文本显示单行格式格式时 RichTextBox ,通常使用此属性。 可以使用此属性来限制在控件中输入的邮政编码和电话号码等值的文本长度,或限制在数据库中输入数据时输入的文本长度。 可以将输入控件的文本限制为数据库中相应字段的最大长度。

注意

在代码中,可以将 属性的值 Text 设置为长度大于 属性指定的 MaxLength 值的值。 此属性仅影响在运行时输入控件的文本。

适用于

另请参阅