RichTextBox.GetCharIndexFromPosition(Point) Méthode

Définition

Récupère l'index du caractère le plus proche de l'emplacement spécifié.

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

Paramètres

pt
Point

Emplacement où rechercher.

Retours

Index de base zéro du caractère situé à l'emplacement spécifié.

Exemples

L’exemple de code suivant montre comment utiliser la GetCharIndexFromPosition méthode avec la Find méthode pour rechercher une chaîne spécifique dans un RichTextBox contrôle et afficher l’index de caractères où se trouve la chaîne trouvée dans le RichTextBox contrôle. L’exemple recherche le mot « brown » dans le contenu du contrôle et retourne la position d’index de caractères où se trouve la chaîne de recherche. Cet exemple nécessite que vous disposiez d’un formulaire qui contient un RichTextBox contrôle nommé richTextBox1 qui contient du texte. Il nécessite également que le code de l’exemple soit connecté à l’événement MouseDown de .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

Remarques

Cette méthode retourne l’index de caractères le plus proche de la position spécifiée dans le pt paramètre. L’index de caractères est un index de base zéro de texte dans le contrôle, y compris les espaces. Vous pouvez utiliser cette méthode pour déterminer où, dans le texte, l’utilisateur a la souris en passant les coordonnées de la souris à cette méthode. Cela peut être utile si vous souhaitez effectuer des tâches lorsque l’utilisateur place le pointeur de la souris sur un mot dans le texte du contrôle.

S’applique à

Voir aussi