RichTextBox.GetLineFromCharIndex メソッド
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim instance As RichTextBox Dim index As Integer Dim returnValue As Integer returnValue = instance.GetLineFromCharIndex(index)
戻り値
文字インデックスを含む行の 0 から始まる行番号。

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

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


RichTextBox クラス
RichTextBox メンバ
System.Windows.Forms 名前空間
GetCharIndexFromPosition
GetPositionFromCharIndex
Weblioに収録されているすべての辞書からRichTextBox.GetLineFromCharIndex メソッドを検索する場合は、下記のリンクをクリックしてください。

- RichTextBox.GetLineFromCharIndex メソッドのページへのリンク