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 を返しますWordWrapfalse設定されている場合は、行のどの部分次行には折り返さず、このメソッド指定した文字インデックスに対して 0 を返します。このメソッド使用して特定の文字インデックス含まれている行を確認できます。たとえば、Find メソッド呼び出してテキスト検索し文字見つかった位置文字インデックス取得できますFind メソッド返され文字インデックス指定してこのメソッド呼び出すと、文字がどの行で見つかったのかを確認できます

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

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

メモメモ

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

使用例使用例

GetLineFromCharIndex メソッド使用するコード例次に示します。この例を実行するには、RichTextBox1 という名前の RichTextBox コントロールButton1 という名前のボタン、および TextBox1TextBox2 という名前の 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


このページでは「.NET Framework クラス ライブラリ リファレンス」からRichTextBox.GetLineFromCharIndex メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からRichTextBox.GetLineFromCharIndex メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からRichTextBox.GetLineFromCharIndex メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

RichTextBox.GetLineFromCharIndex メソッドのお隣キーワード
検索ランキング

   

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



RichTextBox.GetLineFromCharIndex メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS