RichTextBox.GetCharIndexFromPosition(Point) Metoda

Definicja

Pobiera indeks znaku najbliżej określonej lokalizacji.

public:
 int GetCharIndexFromPosition(System::Drawing::Point pt);
public:
 override int GetCharIndexFromPosition(System::Drawing::Point pt);
public int GetCharIndexFromPosition (System.Drawing.Point pt);
public override int GetCharIndexFromPosition (System.Drawing.Point pt);
member this.GetCharIndexFromPosition : System.Drawing.Point -> int
override this.GetCharIndexFromPosition : System.Drawing.Point -> int
Public Function GetCharIndexFromPosition (pt As Point) As Integer
Public Overrides Function GetCharIndexFromPosition (pt As Point) As Integer

Parametry

pt
Point

Lokalizacja do wyszukania.

Zwraca

Indeks znaków oparty na zerach w określonej lokalizacji.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać GetCharIndexFromPosition metody z Find metodą do wyszukiwania określonego ciągu w kontrolce RichTextBox i wyświetlania indeksu znaków, w którym znajduje się znaleziony ciąg w kontrolce RichTextBox . W przykładzie wyszukiwany jest wyraz "brown" w obrębie zawartości kontrolki i zwraca pozycję indeksu znaków, w której znajduje się ciąg wyszukiwania. Ten przykład wymaga formularza zawierającego kontrolkę o nazwie richTextBox1 zawierającej RichTextBox tekst. Wymaga również, aby MouseDown kod w przykładzie był połączony ze zdarzeniem .RichTextBox

private:
   void richTextBox1_MouseDown( Object^ /*sender*/, System::Windows::Forms::MouseEventArgs^ e )
   {
      // Declare the string to search for in the control.
      String^ searchString = "brown";

      // Determine whether the user clicks the left mouse button and whether it is a double click.
      if ( e->Clicks == 1 && e->Button == ::MouseButtons::Left )
      {
         // Obtain the character index where the user clicks on the control.
         int positionToSearch = richTextBox1->GetCharIndexFromPosition( Point(e->X,e->Y) );

         // Search for the search string text within the control from the point the user clicked.
         int textLocation = richTextBox1->Find( searchString, positionToSearch, RichTextBoxFinds::None );

         // If the search string is found (value greater than -1), display the index the string was found at.
         if ( textLocation >= 0 )
            MessageBox::Show( String::Format( "The search string was found at character index {0}.", textLocation ) ); // Display a message box alerting the user that the text was not found.
         else
            MessageBox::Show( "The search string was not found within the text of the control." );
      }
   }
private void richTextBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    // Declare the string to search for in the control.
    string searchString = "brown";

    // Determine whether the user clicks the left mouse button and whether it is a double click.
    if (e.Clicks == 1 && e.Button == MouseButtons.Left)
    {
        // Obtain the character index where the user clicks on the control.
        int positionToSearch = richTextBox1.GetCharIndexFromPosition(new Point(e.X, e.Y));
        // Search for the search string text within the control from the point the user clicked.
        int textLocation = richTextBox1.Find(searchString, positionToSearch, RichTextBoxFinds.None);

        // If the search string is found (value greater than -1), display the index the string was found at.
        if (textLocation >= 0)
            MessageBox.Show("The search string was found at character index " + textLocation.ToString() + ".");
        else
            // Display a message box alerting the user that the text was not found.
            MessageBox.Show("The search string was not found within the text of the control.");
    }
}
Private Sub richTextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles richTextBox1.MouseDown
    ' Declare the string to search for in the control.
    Dim searchString As String = "brown"

    ' Determine whether the user clicks the left mouse button and whether it is a double click.
    If e.Clicks = 1 And e.Button = MouseButtons.Left Then
        ' Obtain the character index where the user clicks on the control.
        Dim positionToSearch As Integer = richTextBox1.GetCharIndexFromPosition(New Point(e.X, e.Y))
        ' Search for the search string text within the control from the point the user clicked.
        Dim textLocation As Integer = richTextBox1.Find(searchString, positionToSearch, RichTextBoxFinds.None)

        ' If the search string is found (value greater than -1), display the index the string was found at.
        If textLocation >= 0 Then
            MessageBox.Show(("The search string was found at character index " + textLocation.ToString() + "."))
            ' Display a message box alerting the user that the text was not found.
        Else
            MessageBox.Show("The search string was not found within the text of the control.")
        End If
    End If
End Sub

Uwagi

Ta metoda zwraca indeks znaków, który znajduje się najbliżej pozycji określonej w parametrze pt . Indeks znaków jest indeksem tekstowym na podstawie zera w kontrolce, w tym spacjami. Za pomocą tej metody można określić, gdzie w tekście użytkownik ma wskaźnik myszy, przekazując współrzędne myszy do tej metody. Może to być przydatne, jeśli chcesz wykonać zadania, gdy użytkownik spoczywa wskaźnik myszy nad wyrazem w tekście kontrolki.

Dotyczy

Zobacz też