Results 1 to 6 of 6

Thread: [RESOLVED] Word Under Mouse in RichtextBox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    99

    Resolved [RESOLVED] Word Under Mouse in RichtextBox

    i need some code to get the word under mouse in richtextbox after a search i found this code but it is good but dont working for first and last word after a newline in richtextbox anyone help me ???


    Code:
    Public Function GetWordUnderMouse(ByRef Rtf As System.Windows.Forms.RichTextBox, ByVal X As Integer, ByVal Y As Integer) As String
            On Error Resume Next
            Dim POINT As System.Drawing.Point = New System.Drawing.Point()
            Dim Pos As Integer, i As Integer, lStart As Integer, lEnd As Integer
            Dim lLen As Integer, sTxt As String, sChr As String
            '
            POINT.X = X
            POINT.Y = Y
            GetWordUnderMouse = vbNullString
            '
            With Rtf
                lLen = Len(.Text)
                sTxt = .Text
                Pos = Rtf.GetCharIndexFromPosition(POINT)
                If Pos > 0 Then
                    For i = Pos To 1 Step -1
                        sChr = Mid(sTxt, i, 1)
                        If sChr = " " Or sChr = Chr(13) Or sChr = ControlChars.Lf Or i = 1 Then
                            'if the starting character is vbcrlf then
                            'we want to chop that off
                            If sChr = Chr(13) Then
                                lStart = (i + 2)
                            ElseIf sChr = " " Then
                                lStart = i
                            Else
                                lStart = (i + 2)
                            End If
                            Exit For
                        End If
                    Next i
                    For i = Pos To lLen
                        If Mid(sTxt, i, 1) = " " Or Mid(sTxt, i, 1) = Chr(13) Or i = lLen Then
                            lEnd = i + 1
                            Exit For
                        End If
                    Next i
                    If lEnd >= lStart Then
                        GetWordUnderMouse = Trim(Mid(sTxt, lStart, lEnd - lStart))
                    End If
                End If
            End With
        End Function

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Word Under Mouse in RichtextBox

    Try this
    vb Code:
    1. Private Function GetWord(ByVal strText As String, ByVal lngCursorPosition As Long) As String
    2.  
    3.         Dim j As Long
    4.         Dim lngRightSpacePos As Long
    5.  
    6.         ' Get the position of first <Space> or <NewLine> comes AFTER the cursor position.
    7.         For j = lngCursorPosition + 1 To Len(strText)
    8.             If Mid$(strText, j, 1) = " " Or Mid$(strText, j, 2) = vbNewLine Then
    9.                 Exit For
    10.             End If
    11.         Next
    12.         lngRightSpacePos = j ' Save the position
    13.  
    14.  
    15.         ' Get the position of first <Space> or <NewLine> comes BEFORE the cursor position.
    16.         For j = lngCursorPosition To 1 Step -1
    17.             If Mid$(strText, j, 1) = " " Or Mid$(strText, j, 2) = vbNewLine Then
    18.                 Exit For
    19.             End If
    20.         Next
    21.  
    22.         GetWord = Mid$(strText, j + 1, lngRightSpacePos - j - 1)
    23.  
    24.     End Function

    Usage

    vb Code:
    1. Me.Text = GetWord(RichTextBox1.Text, RichTextBox1.SelectionStart)

    I posted that solution before here http://www.vbforums.com/showthread.php?t=672153



  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    99

    Re: Word Under Mouse in RichtextBox

    Thanks,
    i saw that earlier but i need to use that word in contextmenustrip for right click menu so it dont work for me for that.

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Word Under Mouse in RichtextBox

    Use this code for the event RichTextBox1_MouseDown

    vb Code:
    1. Private Sub RichTextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseDown
    2.  
    3.         If e.Button = Windows.Forms.MouseButtons.Right Then
    4.             RichTextBox1.Select(RichTextBox1.GetCharIndexFromPosition(New Point(e.X, e.Y)), 0)
    5.         End If
    6.  
    7.         Me.Text = GetWord(RichTextBox1.Text, RichTextBox1.SelectionStart)
    8.  
    9.     End Sub



  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    99

    Re: [RESOLVED] Word Under Mouse in RichtextBox

    thank you very much it work great now

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [RESOLVED] Word Under Mouse in RichtextBox

    that GetWord function is legacy code.
    you can do it this way with regex + vb.net string manipulation methods:

    vb Code:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub RichTextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseMove
    6.         Dim cursorAt As Integer = RichTextBox1.GetCharIndexFromPosition(e.Location)
    7.         If cursorAt = 0 Then Return
    8.         Dim wordEnd As Integer = New Regex("[\s$]{1}").Match(RichTextBox1.Text.Substring(cursorAt)).Index
    9.         Dim isEnd As Boolean = Not New Regex("\s").IsMatch(RichTextBox1.Text.Substring(cursorAt))
    10.         Dim wordStart As Integer = New Regex("[\s^]{1}", RegexOptions.RightToLeft).Match(RichTextBox1.Text.Substring(0, cursorAt)).Index
    11.         If isEnd Then
    12.             Debug.Print(RichTextBox1.Text.Substring(wordStart).Trim)
    13.         Else
    14.             Debug.Print(RichTextBox1.Text.Substring(wordStart, cursorAt - wordStart + wordEnd).Trim)
    15.         End If
    16.     End Sub
    17.  
    18. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width