次の方法で共有


RichTextBox.MaxLength プロパティ

ユーザーがリッチ テキスト ボックス コントロールに入力または貼り付けできる最大文字数を取得または設定します。

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

構文

'宣言
Public Overrides Property MaxLength As Integer
'使用
Dim instance As RichTextBox
Dim value As Integer

value = instance.MaxLength

instance.MaxLength = value
public override int MaxLength { get; set; }
public:
virtual property int MaxLength {
    int get () override;
    void set (int value) override;
}
/** @property */
public int get_MaxLength ()

/** @property */
public void set_MaxLength (int value)
public override function get MaxLength () : int

public override function set MaxLength (value : int)

プロパティ値

コントロールに入力できる文字数。既定値は MaxValue です。

例外

例外の種類 条件

ArgumentException

プロパティに代入された値が 0 未満です。

解説

このプロパティが 0 に設定されている場合、コントロールに入力できるテキストの最大の長さは 64 KB 分文字になります。このプロパティは、通常、RichTextBox に単一行のリッチ テキスト形式 (RTF: Rich Text Format) テキストを表示するときに使用します。このプロパティを使用すると、郵便番号や電話番号などの値を入力するコントロールのテキストの長さや、データベースにデータを入力するときのテキストの長さを制限できます。コントロールに入力するテキストの長さを、データベースの該当フィールドの最大の長さ以下に制限できます。

注意

コードでは、Text プロパティの値を MaxLength プロパティで指定した値よりも長い値に設定できます。このプロパティは、実行時にコントロールに入力されるテキストにだけ影響します。

使用例

MaxLength プロパティを使用して、RichTextBox コントロールに割り当てられているテキストが MaxLength プロパティで指定した値よりも長いかどうかを確認する方法を次のコード例に示します。この例では、テキストの方が長くない場合は、SelectedText プロパティを使用してコントロールにテキストを割り当てます。この例では、richTextBox1 という名前の RichTextBox コントロールがフォームに追加されており、この例のメソッドがコントロール内に貼り付けられるテキストをパラメータに指定して呼び出される必要があります。また、RichTextBox へのテキスト入力を制限する値を MaxLength プロパティに設定する必要もあります。

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
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:
   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.get_Length() > richTextBox1.get_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.set_SelectedText(textToAdd);
    }
} //AddMyText 

プラットフォーム

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

バージョン情報

.NET Framework

サポート対象 : 2.0、1.1、1.0

参照

関連項目

RichTextBox クラス
RichTextBox メンバ
System.Windows.Forms 名前空間
MaxLength