Bagikan melalui


RichTextBox.GetLineFromCharIndex(Int32) Metode

Definisi

Mengambil nomor baris dari posisi karakter yang ditentukan dalam teks RichTextBox kontrol.

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

Parameter

index
Int32

Posisi indeks karakter untuk dicari.

Mengembalikan

Nomor baris berbasis nol tempat indeks karakter berada.

Contoh

Contoh kode berikut menunjukkan menggunakan GetLineFromCharIndex metode . Untuk menjalankan contoh, tempelkan kode berikut dalam formulir yang berisi RichTextBox kontrol bernama RichTextBox1, tombol bernama Button1 dan dua kotak teks bernama TextBox1 dan TextBox2. Saat contoh berjalan, masukkan string pencarian di TextBox2 dan klik tombol untuk mendapatkan hasil pencarian.

// 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

Keterangan

Metode ini memungkinkan Anda menentukan nomor baris berdasarkan indeks karakter yang ditentukan dalam index parameter metode . Baris pertama teks dalam kontrol mengembalikan nilai nol. Metode GetLineFromCharIndex mengembalikan nomor baris fisik tempat karakter terindeks berada dalam kontrol. Misalnya, jika sebagian dari baris logis pertama teks dalam kontrol membungkus ke baris berikutnya, GetLineFromCharIndex metode mengembalikan 1 jika karakter pada indeks karakter yang ditentukan telah dibungkus ke baris fisik kedua. Jika WordWrap diatur ke false, tidak ada bagian baris yang dibungkus ke baris berikutnya, dan metode mengembalikan 0 untuk indeks karakter yang ditentukan. Anda dapat menggunakan metode ini untuk menentukan garis mana indeks karakter tertentu berada di dalamnya. Misalnya, setelah memanggil Find metode untuk mencari teks, Anda dapat memperoleh indeks karakter ke tempat hasil pencarian ditemukan. Anda dapat memanggil metode ini dengan indeks karakter yang dikembalikan oleh Find metode untuk menentukan baris mana kata yang ditemukan.

Dalam kasus tertentu, GetLineFromCharIndex tidak melemparkan pengecualian ketika index parameter adalah nilai yang tidak valid. Contohnya:

Dalam kasus ini, validasi input sebelum memanggil GetLineFromCharIndex.

Catatan

Jika indeks karakter yang ditentukan dalam parameter berada di index luar jumlah baris yang tersedia yang terkandung dalam kontrol, nomor baris terakhir dikembalikan.

Berlaku untuk

Lihat juga