RichTextBox.GetLineFromCharIndex メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > RichTextBox.GetLineFromCharIndex メソッドの意味・解説 

RichTextBox.GetLineFromCharIndex メソッド

RichTextBox コントロールのテキスト内の指定した文字位置から行番号を取得します。

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

Public Overrides Function
 GetLineFromCharIndex ( _
    index As Integer _
) As Integer
Dim instance As RichTextBox
Dim index As Integer
Dim returnValue As Integer

returnValue = instance.GetLineFromCharIndex(index)
public override int GetLineFromCharIndex (
    int index
)
public:
virtual int GetLineFromCharIndex (
    int index
) override
public int GetLineFromCharIndex (
    int index
)
public override function GetLineFromCharIndex
 (
    index : int
) : int

パラメータ

index

検索する文字インデックスの位置。

戻り値
文字インデックスを含む行の 0 から始まる行番号。

解説解説

このメソッドを使用すると、その index パラメータに指定した文字インデックスを基に行番号を確認できます。コントロール内のテキストの最初の行は、値 0 を返します。GetLineFromCharIndex メソッドは、コントロール内でインデックス付き文字が検索された物理的な行番号を返します。たとえば、コントロール内のテキストの最初の論理行の一部が次の行に折り返している場合、指定したインデックス位置にある文字が 2 番目の物理行に折り返していれば、GetLineFromCharIndex メソッドは 1 を返します。WordWrap が false に設定されている場合は、行のどの部分も次行には折り返さず、このメソッドは指定した文字インデックスに対して 0 を返します。このメソッドを使用して、特定の文字インデックスが含まれている行を確認できます。たとえば、Find メソッドを呼び出してテキストを検索し、文字が見つかった位置の文字インデックスを取得できます。Find メソッドで返された文字インデックスを指定してこのメソッドを呼び出すと、文字がどの行で見つかったのかを確認できます。

場合によっては、index パラメータの値が無効であっても、GetLineFromCharIndex が例外をスローしないことがあります。次に例を示します。

このような場合は、GetLineFromCharIndex を呼び出す前に入力を検証します。

メモメモ

index パラメータに指定した文字インデックスが、コントロール内に含まれている行数を超えた位置を示している場合は、最後の行番号が返されます。

使用例使用例

GetLineFromCharIndex メソッドを使用するコード例を次に示します。この例を実行するには、RichTextBox1 という名前の RichTextBox コントロール、Button1 という名前のボタン、および TextBox1 と TextBox2 という名前の 2 つのテキスト ボックスが配置されているフォームに、次のコードを貼り付けます。例を実行するときは、TextBox2 に検索文字列を入力し、ボタンをクリックして検索結果を取得します。

' This method demonstrates retrieving line numbers that 
' indicate the location of a particular word
' contained in a RichTextBox. The line numbers are zero-based.

Private Sub Button1_Click(ByVal
 sender As System.Object, _ 
    ByVal e As System.EventArgs) Handles
 Button1.Click

    ' Reset the results box.
    TextBox1.Text = ""

    ' Get the word to search from from TextBox2.
    Dim searchWord As String
 = TextBox2.Text

    Dim index As Integer
 = 0

    'Declare an ArrayList to store line numbers.
    Dim lineList As New
 System.Collections.ArrayList
    Do
        ' Find occurrences of the search word, incrementing  
        ' the start index. 
        index = RichTextBox1.Find(searchWord, index + 1, _
            RichTextBoxFinds.MatchCase)
        If (index <> -1) Then

            ' Find the word's line number and add the line 
            'number to the arrayList. 
            lineList.Add(RichTextBox1.GetLineFromCharIndex(index))
        End If
    Loop While (index <> -1)

    ' Iterate through the list and display the line numbers in TextBox1.
    Dim myEnumerator As System.Collections.IEnumerator
 = _
        lineList.GetEnumerator()
    If lineList.Count <= 0 Then
        TextBox1.Text = searchWord & " was not found"
    Else
        TextBox1.SelectedText = searchWord & " was found on
 line(s):"
        While (myEnumerator.MoveNext)
            TextBox1.SelectedText = myEnumerator.Current & "
 "
        End While
    End If

End Sub
// This method demonstrates retrieving line numbers that 
// indicate the location of a particular word
// contained in a RichTextBox. The line numbers are zero-based.

private void Button1_Click(System.Object sender,
 System.EventArgs e)
{

    // Reset the results box.
    TextBox1.Text = "";

    // Get the word to search from from TextBox2.
    string searchWord = TextBox2.Text;

    int index = 0;

    //Declare an ArrayList to store line numbers.
    System.Collections.ArrayList lineList = new System.Collections.ArrayList();
    do
    {
        // Find occurrences of the search word, incrementing  
        // the start index. 
        index = RichTextBox1.Find(searchWord, index+1, RichTextBoxFinds.MatchCase);
        if (index!=-1)

            // Find the word's line number and add the line 
            // number to the arrayList. 
        {
            lineList.Add(RichTextBox1.GetLineFromCharIndex(index));
        }
    }
    while((index!=-1));

    // Iterate through the list and display the line numbers in TextBox1.
    System.Collections.IEnumerator myEnumerator = lineList.GetEnumerator();
    if (lineList.Count<=0)
    {
        TextBox1.Text = searchWord+" was not found";
    }
    else
    {
        TextBox1.SelectedText = searchWord+" was found on line(s):";
        while (myEnumerator.MoveNext())
        {
            TextBox1.SelectedText = myEnumerator.Current+" ";
        }
    }
}
// This method demonstrates retrieving line numbers that 
// indicate the location of a particular word
// contained in a RichTextBox. The line numbers are zero-based.
void Button1_Click( System::Object^ /*sender*/, System::EventArgs^
 /*e*/ )
{
   
   // Reset the results box.
   TextBox1->Text = "";
   
   // Get the word to search from from TextBox2.
   String^ searchWord = TextBox2->Text;
   int index = 0;
   
   //Declare an ArrayList to store line numbers.
   System::Collections::ArrayList^ lineList = gcnew System::Collections::ArrayList;
   do
   {
      // Find occurrences of the search word, incrementing  
      // the start index. 
      index = RichTextBox1->Find( searchWord, index + 1, RichTextBoxFinds::MatchCase
 );
      if ( index != -1 )
      {
         lineList->Add( RichTextBox1->GetLineFromCharIndex( index ) );
      }
   }
   while ( (index != -1) );

   // Iterate through the list and display the line numbers in TextBox1.
   System::Collections::IEnumerator^ myEnumerator = lineList->GetEnumerator();
   if ( lineList->Count <= 0 )
   {
      TextBox1->Text = searchWord + " was not found";
   }
   else
   {
      TextBox1->SelectedText = searchWord + " was found on line(s):";
      while ( myEnumerator->MoveNext() )
      {
         TextBox1->SelectedText = myEnumerator->Current + " ";
      }
   }
}
// This method demonstrates retrieving line numbers that 
// indicate the location of a particular word
// contained in a RichTextBox. The line numbers are zero-based.
private void button1_Click(Object sender, System.EventArgs
 e)
{
    // Reset the results box.
    textBox1.set_Text("");
    // Get the word to search from from TextBox2.
    String searchWord = textBox2.get_Text();

    int index = 0;
    //Declare an ArrayList to store line numbers.
    System.Collections.ArrayList lineList =
        new System.Collections.ArrayList();
    do {
        // Find occurrences of the search word, incrementing  
        // the start index. 
        index = richTextBox1.Find(searchWord, index + 1,
            RichTextBoxFinds.MatchCase);
        if (index != -1) {
            // Find the word's line number and add the line 
            // number to the arrayList. 
            lineList.Add((System.Int32)richTextBox1.
                GetLineFromCharIndex(index));
        }
    } while (index != -1);
    // Iterate through the list and display the line numbers in TextBox1.
    System.Collections.IEnumerator myEnumerator = lineList.GetEnumerator();
    if (lineList.get_Count() <= 0) {
        textBox1.set_Text(searchWord + " was not found");
    }
    else {
        textBox1.set_SelectedText(searchWord + " was found on line(s):");
        while (myEnumerator.MoveNext()) {
            textBox1.set_SelectedText(myEnumerator.get_Current() + " ");
        }
    }
} //button1_Click
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
RichTextBox クラス
RichTextBox メンバ
System.Windows.Forms 名前空間
GetCharIndexFromPosition
GetPositionFromCharIndex



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

RichTextBox.GetLineFromCharIndex メソッドのお隣キーワード

   

英語⇒日本語
日本語⇒英語
   



RichTextBox.GetLineFromCharIndex メソッドのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS