다음을 통해 공유


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 메서드는 컨트롤에 있는 인덱싱된 문자의 실제 줄 번호를 반환합니다. 예를 들어, 컨트롤에 있는 텍스트의 첫째 논리 줄의 일부가 다음 줄을 래핑하면, 즉 지정된 문자 인덱스에 있는 문자가 실제적으로 둘째 줄을 래핑하면 GetLineFromCharIndex 메서드는 1을 반환합니다. WordWrapfalse로 설정하면 줄의 일부가 다음을 래핑하지 않고 지정된 문자 인덱스에 대해 메서드가 0을 반환합니다. 이 메서드를 사용하면 특정 문자 인덱스가 있는 줄을 확인할 수 있습니다. 예를 들어, Find 메서드를 호출하여 텍스트를 검색한 다음 검색 결과가 발견된 위치에 문자 인덱스를 가져올 수 있습니다. Find 메서드에 의해 반환된 문자 인덱스를 사용하여 이 메서드를 호출하면 해당 단어가 발견된 줄을 확인할 수 있습니다.

일부 경우에는 index 매개 변수의 값이 잘못되었는데도 GetLineFromCharIndex가 예외를 throw하지 않습니다. 예를 들면 다음과 같습니다.

  • index 매개 변수가 MinValue 또는 -1이면 GetLineFromCharIndex는 0을 반환합니다.

  • index 매개 변수가 텍스트 길이 또는 MaxValue이면 GetLineFromCharIndex는 텍스트의 마지막 줄 번호를 반환합니다. 이 번호는 WordWrap 속성 값에 따라 Lines.Length-1과 같지 않을 수도 있습니다.

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

참고

index 매개 변수에 지정된 문자 인덱스가 컨트롤 내의 사용 가능한 줄 번호를 벗어나면 마지막 줄 번호가 반환됩니다.

예제

다음 코드 예제에서는 GetLineFromCharIndex 메서드를 사용하는 방법을 보여 줍니다. 이 예제를 실행하려면 RichTextBox1이라는 RichTextBox 컨트롤, Button1이라는 단추 및 TextBox1TextBox2라는 텍스트 상자 두 개가 포함된 폼에 다음 코드를 붙여넣습니다. 예제가 실행되면 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에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

참고 항목

참조

RichTextBox 클래스
RichTextBox 멤버
System.Windows.Forms 네임스페이스
GetCharIndexFromPosition
GetPositionFromCharIndex