[Tips][RichTextBox] テキストの行数を取得する

スポンサーリンク

RichTextBoxコントロールに表示されているテキストの行数を取得するにはLinesプロパティLengthプロパティを参照します。

下記は、表示されているテキストの行数を取得する例です。

VBの例

' フォームロード時の処理
Private Sub Form8_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'RichTextBoxにデータを表示
    RichTextBox1.Text =
        "1.Red" & ControlChars.NewLine &
        "2.White" & ControlChars.NewLine &
        "3.Yellow" & ControlChars.NewLine &
        "4.Orange" & ControlChars.NewLine &
        "5.Blue" & ControlChars.NewLine &
        "6.Green" & ControlChars.NewLine &
        "7.Purple" & ControlChars.NewLine &
        "8.Pink" & ControlChars.NewLine &
        "9.Gray" & ControlChars.NewLine &
        "10.Black"
End Sub

' [行数を表示]ボタンクリック時の処理
Private Sub btnShowData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowData.Click
    '★★★行数を取得★★★
    Dim intLineCnt As Integer = RichTextBox1.Lines.Length

    MessageBox.Show(intLineCnt & "行です")
End Sub

C#の例

// フォームロード時の処理
private void Form8_Load(object sender, EventArgs e)
{
    // RichTextBoxにデータを表示
    richTextBox1.Text =
        "1.Red\r\n" +
        "2.White\r\n" +
        "3.Yellow\r\n" +
        "4.Orange\r\n" +
        "5.Blue\r\n" +
        "6.Green\r\n" +
        "7.Purple\r\n" +
        "8.Pink\r\n" +
        "9.Gray\r\n" +
        "10.Black";
}

// [行数を表示]ボタンクリック時の処理
private void btnShowData_Click(object sender, EventArgs e)
{
    // ★★★行数を取得★★★
    int intLineCnt = richTextBox1.Lines.Length;

    MessageBox.Show(intLineCnt.ToString());
}
Please follow and like us:

コメント

タイトルとURLをコピーしました