次の方法で共有


RichTextBox.Text プロパティ

リッチ テキスト ボックスの現在のテキストを取得または設定します。

Overrides Public Property Text As String
[C#]
public override string Text {get; set;}
[C++]
public: __property String* get_Text();public: __property void set_Text(String*);
[JScript]
public override function get Text() : String;public override function set Text(String);

プロパティ値

コントロール内に表示するテキスト。

解説

RichTextBox に複数行のテキストを表示するには、 Multiline プロパティを true に設定します。複数行テキスト ボックスのテキストの読み取り、または設定には、 Lines プロパティを使用します。 Text プロパティは、 RichTextBox の内容に適用されている書式設定に関する情報を返しません。RTF (Rich Text Format) 書式コードを取得するには、 Rtf プロパティを使用します。 RichTextBox コントロールに入力できるテキストの文字数が制限されるのは、使用可能なシステム メモリでだけです。

使用例

GetPositionFromCharIndex メソッドと Text プロパティの使用方法を示すコード例を次に示します。この例を実行するには、次のコードをフォームに貼り付けます。そして、フォームのコンストラクタまたは Load メソッドから InitializeRichTextBox メソッドを呼び出します。

[SampleID='System.Windows.Forms.RichTextBoxGetPosition' SnippetID='1']
--------- Languages displayed= cs, vb ---------
--------- cs ---------
--------- Snippet 1 ---------
        private void InitializeRichTextBox()
        {
            // Set textbox's text property.
            this.richTextBox1.Text = "Order Number: 12345\nCustomer Number: " 
                + "4567\n\tItem Number: 12984\n\tQuantity: 1\n\tUnit " 
                + "Price: 1.29\nTotal Price: 1.29\nShipping method: " 
                + "UPS Ground\nTotal Invoice: 4.29";
            // Associate the event-handling method with the
            // MouseDown event.
            this.richTextBox1.MouseDown += 
                new MouseEventHandler(richTextBox1_MouseDown);
        }
        private void richTextBox1_MouseDown(
            object sender, System.Windows.Forms.MouseEventArgs e)
        {
            Int32 searchIndex = -1;
            // Determine whether the button pressed was the right 
            // mouse button.
            if (e.Button == MouseButtons.Right)
            {
                // Get the index of the item number.
                searchIndex = richTextBox1.Text.IndexOf("Item Number:");
                
                if (searchIndex != -1) 
                {
                    // Use the index to retrieve the point at which the 
                    // item number is located. 
                    Point menuPoint = 
                        richTextBox1.GetPositionFromCharIndex(searchIndex);
                    // Add five to both coordinates of the point.
                    menuPoint.X += 5;
                    menuPoint.Y += 5;
                    // Create the context menu.
                    MenuItem[] items = 
                        new MenuItem[]{new MenuItem("Show Item Description"),
                            new MenuItem("Go to Item Number List")};
                                                                                        
                    ContextMenu menu = new ContextMenu(items);
                    // Show the menu at the point found above.
                    menu.Show(this.richTextBox1, menuPoint);
                }
            }
        }
--------- vb ---------
--------- Snippet 1 ---------
    Private Sub InitializeRichTextBox()
        ' Set textbox's text property.
        Me.richTextBox1.Text = "Order Number: 12345" & vbLf _
        & "Customer Number: " & "4567" & vbLf & vbTab _
        & "Item Number: 12984" & vbLf & vbTab & "Quantity: 1" _
        & vbLf & vbTab & "Unit " & "Price: 1.29" & vbLf _
        & "Total Price: 1.29" & vbLf & "Shipping method: " _
        & "UPS Ground" & vbLf & "Total Invoice: 4.29"
    End Sub
    Private Sub richTextBox1_MouseDown(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles richTextBox1.MouseDown
        Dim searchIndex As Int32 = -1
        ' Determine whether the button pressed was the right 
        ' mouse button.
        If e.Button = MouseButtons.Right Then
            ' Get the index of the item number.
            searchIndex = richTextBox1.Text.IndexOf("Item Number:")
            If searchIndex <> -1 Then
                ' Use the index to retrieve the point at which the 
                ' item number is located. 
                Dim menuPoint As Point = _
                richTextBox1.GetPositionFromCharIndex(searchIndex)
                ' Add five to both coordinates of the point.
                menuPoint.X += 5
                menuPoint.Y += 5
                ' Create the context menu.
                Dim items() As MenuItem = _
                    {New MenuItem("Show Item Description"), _
                    New MenuItem("Go to Item Number List")}
                Dim menu As New ContextMenu(items)
                ' Show the menu at the point found above.
                menu.Show(Me.richTextBox1, menuPoint)
            End If
        End If
    End Sub
   End Class

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

RichTextBox クラス | RichTextBox メンバ | System.Windows.Forms 名前空間 | Lines | Multiline