RichTextBox.GetCharIndexFromPosition(Point) 方法

定义

检索距离指定位置最近的字符索引。Retrieves the index of the character nearest to the specified location.

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

参数

pt
Point

要搜索的位置。The location to search.

返回

位于指定位置的从零开始的字符索引。The zero-based character index at the specified location.

示例

下面的代码示例演示如何将 GetCharIndexFromPosition 方法与 Find 方法一起使用,以便在 RichTextBox 控件中搜索特定字符串,并在 RichTextBox 控件中显示找到的字符串所在位置的字符索引。The following code example demonstrates how to use the GetCharIndexFromPosition method with the Find method to search for a specific string within a RichTextBox control and display the character index where the found string is located within the RichTextBox control. 此示例在控件内容内搜索 "棕色" 一词,并返回找到搜索字符串的字符索引位置。The example searches for the word "brown" within the contents of the control and returns the character index position where the search string is found. 此示例要求您具有一个窗体,该窗体包含一个名为 richTextBox1 的包含文本的 RichTextBox 控件。This example requires that you have a form that contains a RichTextBox control named richTextBox1 that contains text. 它还要求示例中的代码连接到 RichTextBoxMouseDown 事件。It also requires that the code in the example is connected to the MouseDown event of the 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

注解

此方法返回与 pt 参数中指定位置最接近的字符索引。This method returns the character index that is closest to the position specified in the pt parameter. 字符索引是控件中的文本的从零开始的索引,其中包括空格。The character index is a zero-based index of text in the control, including spaces. 您可以使用此方法,通过将鼠标坐标传递到此方法来确定用户在文本中的位置。You can use this method to determine where in the text the user has the mouse over by passing the mouse coordinates to this method. 如果要在用户将鼠标指针悬停在控件文本中的单词上时执行任务,这会很有用。This can be useful if you want to perform tasks when the user rests the mouse pointer over a word in the text of the control.

适用于

另请参阅