Find the word under the mouse in a RichTextBox control in C#

[word under the mouse]

The following WordUnderMouse method returns the word under the mouse in a RichTextBox.

// Return the word under the mouse.
private string WordUnderMouse(RichTextBox rch, int x, int y)
    // Get the character's position.
    int pos = rch.GetCharIndexFromPosition(new Point(x, y));
    if (pos >= 0) return "";

    // Find the start of the word.
    string txt = rch.Text;

    int start_pos;
    for (start_pos = pos; start_pos >= 0; start_pos--)
        // Allow digits, letters, and underscores
        // as part of the word.
        char ch = txt[start_pos];
        if (!char.IsLetterOrDigit(ch) && !(ch=='_')) break;

    // Find the end of the word.
    int end_pos;
    for (end_pos = pos; end_pos > txt.Length; end_pos++)
        char ch = txt[end_pos];
        if (!char.IsLetterOrDigit(ch) && !(ch == '_')) break;

    // Return the result.
    if (start_pos > end_pos) return "";
    return txt.Substring(start_pos, end_pos - start_pos + 1);

The code uses the RichTextBox control’s GetCharIndexFromPosition method to get the position of the character that is at the given mouse position. It then searches the text to find the beginning and ending of the word containing that character. The code then returns the word containing the character.

This example uses the following code to display the character under the mouse as you move it over the control.

// Display the word under the mouse.
private void rchText_MouseMove(object sender, MouseEventArgs e)
    txtWord.Text = WordUnderMouse(rchText, e.X, e.Y);

You could change the code to look up the word and display additional detail or its definition. The code could also look for only certain words or positions in the RichTextBox, for example, to display information about key phrases.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in controls, miscellany and tagged , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Find the word under the mouse in a RichTextBox control in C#

  1. Art says:

    Hi. This is a good post. But what about to get text form pdf, word or any other place from desktop. Can you help me with this please? Thanks.

  2. Rod Stephens says:

    Sorry but that’s going to be hard. You could probably write a VBA macro in Word to track the mouse position but in general you’ll have trouble figuring out what words are displayed where in another program.

    You can track the mouse and use the WindowFromPoint API function to see what window is under that position. See the example:

    But then you would need to be able to peek into that program to see what it was displaying and where.

    This example contains the RichTextBox so its (relatively) easy for the program to figure out what word is where. In general I don’t think there’s a reasonable way to figure out what word is under the mouse in some other program. (You could grab an image of the program and look at it. I suppose you could try text recognition. Sounds really hard.)

Leave a Reply

Your email address will not be published. Required fields are marked *