通过


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

例外

分配给该属性的值小于 0。

示例

下面的代码示例演示如何使用 MaxLength 属性来确定分配给 RichTextBox 控件的文本是否大于分配给 MaxLength 该属性的值。 如果文本不较大,则本示例使用 SelectedText 属性将文本分配给控件。 本示例要求已将名为/> 的控件添加到窗体中,并且示例中的方法使用提供给要粘贴到控件中的参数的文本进行调用。 该示例还要求该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。 此属性通常用于 RichTextBox 显示单行格式格式(RTF)文本。 可以使用此属性限制在控件中输入的值(如邮政编码和电话号码)中的文本长度,或限制在数据库中输入数据时输入的文本长度。 可以将输入到控件中的文本限制为数据库中相应字段的最大长度。

注释

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

适用于

另请参阅