RichTextBox.MaxLength 屬性

定義

取得或設定使用者能夠輸入或貼入 Rich Text Box 控制項中的最大字元數。

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 使用 屬性將文字指派給 控制項。 這個範例會要求 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) 文字的單行 RTF 格式 (時,通常會使用 RichTextBox 這個屬性。 您可以使用此屬性來限制控制項中輸入的文字長度,例如郵遞區號和電話號碼,或限制在資料庫中輸入資料時輸入的文字長度。 您可以將控制項中輸入的文字限制為資料庫中對應欄位的最大長度。

注意

在程式碼中 Text ,您可以將 屬性的值設定為長度大於 屬性所 MaxLength 指定值的值。 此屬性只會影響在執行時間輸入控制項中的文字。

適用於

另請參閱