Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

RichTextBox.GetCharIndexFromPosition Method

Gets the index of the character nearest to the specified location.

[Visual Basic]
Public Function GetCharIndexFromPosition( _
   ByVal pt As Point _
) As Integer
[C#]
public int GetCharIndexFromPosition(
 Point pt
);
[C++]
public: int GetCharIndexFromPosition(
 Point pt
);
[JScript]
public function GetCharIndexFromPosition(
   pt : Point
) : int;

Parameters

pt
The location to search.

Return Value

The zero-based character index at the specified location.

Remarks

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 hovers the mouse pointer over a word in the text of the control.

Example

[Visual Basic, C#, C++] The following 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. This example assumes that you have a form that contains a RichTextBox control, named richTextBox1, that contains text. It also assumes that the code in the example is connected to the MouseDown event of the RichTextBox.

[Visual Basic] 
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

[C#] 
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.");
    }
}

[C++] 
private:
    void richTextBox1_MouseDown(Object* /*sender*/, System::Windows::Forms::MouseEventArgs* e)
    {
        // Declare the string to search for in the control.
        String* searchString = S"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( S"The search string was found at character index {0}.", __box(textLocation)));
            else
                // Display a message box alerting the user that the text was not found.
                MessageBox::Show(S"The search string was not found within the text of the control.");
        }
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

RichTextBox Class | RichTextBox Members | System.Windows.Forms Namespace | GetCharFromPosition | GetLineFromCharIndex | GetPositionFromCharIndex

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft