共用方式為


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 屬性將文字指派給控制項。 此範例要求 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 字元。 此特性通常用於 RichTextBox 顯示一行富文字格式(RTF)文字時。 你可以利用此特性限制控制中輸入的文字長度,例如郵遞區號和電話號碼,或限制資料輸入資料庫時輸入的文字長度。 你可以限制輸入控制項的文字長度為資料庫中對應欄位的最大長度。

備註

在程式碼中,你可以將屬性的值 Text 設定為長度超過屬性 MaxLength 指定的值。 此特性僅影響執行時輸入控制項的文字。

適用於

另請參閱