When you move the mouse cursor over hyperlink in some browser the cursor is changed and when you remove it from the hyperlink the cursor is changed to default cursor.Is it possible to have the same functionality in .Net?I'm talking about RichTextBox specifically.I mean if i move the cursor over a certain word i want a different cursor over this word.I've been asking this question in maybe...i don't know,maybe 10 other message boards and doesn't get adequate answer.Is it really impossible?

Dani AI

Generated

As asked by , the goal is changing the cursor when the mouse is over a specific word inside a WinForms RichTextBox. was right that it feels "hackish" compared with hyperlink detection, and pointed toward native APIs. A simpler managed approach avoids P/Invoke: map the mouse point to a character with RichTextBox.GetCharIndexFromPosition, expand left/right to find the word bounds, then update the control cursor (for the API reference see RichTextBox.GetCharIndexFromPosition).

A compact VB.NET MouseMove example (tracks the last word to avoid flicker):

Private prevStart As Integer = -1
Private prevEnd As Integer = -1
Private interestingWords As New List(Of String) From {"Important", "LinkMe"}

Private Sub RichTextBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseMove
    Dim idx = RichTextBox1.GetCharIndexFromPosition(e.Location)
    If idx < 0 OrElse idx >= RichTextBox1.TextLength Then
        RichTextBox1.Cursor = Cursors.IBeam
        Return
    End If

    Dim txt = RichTextBox1.Text
    Dim s = idx
    While s > 0 AndAlso Not Char.IsWhiteSpace(txt(s - 1)) AndAlso Not Char.IsPunctuation(txt(s - 1))
        s -= 1
    End While
    Dim t = idx
    While t < txt.Length - 1 AndAlso Not Char.IsWhiteSpace(txt(t + 1)) AndAlso Not Char.IsPunctuation(txt(t + 1))
        t += 1
    End While

    If s = prevStart AndAlso t = prevEnd Then Return
    prevStart = s : prevEnd = t
    Dim word = txt.Substring(s, t - s + 1)
    If interestingWords.Exists(Function(w) String.Equals(w, word, StringComparison.OrdinalIgnoreCase)) Then
        RichTextBox1.Cursor = Cursors.Hand
    Else
        RichTextBox1.Cursor = Cursors.IBeam
    End If
End Sub

Notes: throttle heavy checks if the document is large; update the cursor only when the word changes to reduce flicker; RichTextBox.DetectUrls may change cursor behavior for real links. For general cursor handling see the control cursor docs (Control.Cursor).

Recommended Answers

All 2 Replies

Member Avatar for Member #46692

Of course that is possible. But I've never heard of anyone doing it over certain words.

In that case a more hackish solution is the likely avenue to explore.

you can, there is a windows API used to get the word on which the cursor exists.
you can see any open source dictionary that translates words whenever you move your cursor on like EasyLingo

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.