RichTextBox.GetLineFromCharIndex(Int32) 메서드

정의

RichTextBox 컨트롤의 텍스트 내에서 지정된 문자 위치의 줄 번호를 검색합니다.

public:
 int GetLineFromCharIndex(int index);
public:
 override int GetLineFromCharIndex(int index);
public int GetLineFromCharIndex (int index);
public override int GetLineFromCharIndex (int index);
member this.GetLineFromCharIndex : int -> int
override this.GetLineFromCharIndex : int -> int
Public Function GetLineFromCharIndex (index As Integer) As Integer
Public Overrides Function GetLineFromCharIndex (index As Integer) As Integer

매개 변수

index
Int32

검색할 문자 인덱스 위치입니다.

반환

문자 인덱스가 있는 0부터 시작하는 줄 번호입니다.

예제

다음 코드 예제는 GetLineFromCharIndex 메서드. 예제를 실행하려면 라는 컨트롤, 라는 Button1 단추 및 및 TextBox2라는 RichTextBox1두 개의 텍스트 상자가 포함된 RichTextBox 양식에 다음 코드를 붙여넣습니다TextBox1. 예제가 실행 중이면 에서 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.
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(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.

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

설명

이 메서드를 사용하면 메서드의 매개 변수에 지정된 index 문자 인덱스를 기반으로 줄 번호를 확인할 수 있습니다. 컨트롤의 첫 번째 텍스트 줄은 값 0을 반환합니다. 메서드는 GetLineFromCharIndex 인덱싱된 문자가 컨트롤 내에 있는 실제 줄 번호를 반환합니다. 예를 들어 컨트롤의 첫 번째 논리 텍스트 줄 부분이 다음 줄로 래핑되는 경우 지정된 문자 인덱스의 문자가 두 번째 실제 줄 GetLineFromCharIndex 로 래핑된 경우 메서드는 1을 반환합니다. 가 로 false설정된 경우 WordWrap 줄의 일부가 다음 줄로 래핑되지 않고 메서드는 지정된 문자 인덱스에 대해 0을 반환합니다. 이 메서드를 사용하여 특정 문자 인덱스가 있는 줄을 확인할 수 있습니다. 예를 들어 메서드를 Find 호출하여 텍스트를 검색한 후 검색 결과가 있는 위치에 대한 문자 인덱스도 가져올 수 있습니다. 메서드에서 반환된 문자 인덱스로 이 메서드를 Find 호출하여 단어가 발견된 줄을 확인할 수 있습니다.

경우에 따라 매개 변수가 GetLineFromCharIndex 잘못된 값인 경우 에서 예외를 index throw하지 않습니다. 예를 들면 다음과 같습니다.

이러한 경우 를 호출 GetLineFromCharIndex하기 전에 입력의 유효성을 검사합니다.

참고

매개 변수에 index 지정된 문자 인덱스가 컨트롤에 포함된 사용 가능한 줄 수를 초과하면 마지막 줄 번호가 반환됩니다.

적용 대상

추가 정보