|
-
Mar 20th, 2012, 03:46 AM
#1
Thread Starter
Lively Member
[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
-
Mar 20th, 2012, 06:11 AM
#2
Re: Word Under Mouse in RichtextBox
Try this
vb Code:
Private Function GetWord(ByVal strText As String, ByVal lngCursorPosition As Long) As String Dim j As Long Dim lngRightSpacePos As Long ' Get the position of first <Space> or <NewLine> comes AFTER the cursor position. For j = lngCursorPosition + 1 To Len(strText) If Mid$(strText, j, 1) = " " Or Mid$(strText, j, 2) = vbNewLine Then Exit For End If Next lngRightSpacePos = j ' Save the position ' Get the position of first <Space> or <NewLine> comes BEFORE the cursor position. For j = lngCursorPosition To 1 Step -1 If Mid$(strText, j, 1) = " " Or Mid$(strText, j, 2) = vbNewLine Then Exit For End If Next GetWord = Mid$(strText, j + 1, lngRightSpacePos - j - 1) End Function
Usage
vb Code:
Me.Text = GetWord(RichTextBox1.Text, RichTextBox1.SelectionStart)
I posted that solution before here http://www.vbforums.com/showthread.php?t=672153
-
Mar 20th, 2012, 07:11 AM
#3
Thread Starter
Lively Member
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.
-
Mar 20th, 2012, 07:34 AM
#4
Re: Word Under Mouse in RichtextBox
Use this code for the event RichTextBox1_MouseDown
vb Code:
Private Sub RichTextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseDown If e.Button = Windows.Forms.MouseButtons.Right Then RichTextBox1.Select(RichTextBox1.GetCharIndexFromPosition(New Point(e.X, e.Y)), 0) End If Me.Text = GetWord(RichTextBox1.Text, RichTextBox1.SelectionStart) End Sub
-
Mar 20th, 2012, 07:40 AM
#5
Thread Starter
Lively Member
Re: [RESOLVED] Word Under Mouse in RichtextBox
thank you very much it work great now
-
Mar 20th, 2012, 09:44 AM
#6
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:
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub RichTextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseMove
Dim cursorAt As Integer = RichTextBox1.GetCharIndexFromPosition(e.Location)
If cursorAt = 0 Then Return
Dim wordEnd As Integer = New Regex("[\s$]{1}").Match(RichTextBox1.Text.Substring(cursorAt)).Index
Dim isEnd As Boolean = Not New Regex("\s").IsMatch(RichTextBox1.Text.Substring(cursorAt))
Dim wordStart As Integer = New Regex("[\s^]{1}", RegexOptions.RightToLeft).Match(RichTextBox1.Text.Substring(0, cursorAt)).Index
If isEnd Then
Debug.Print(RichTextBox1.Text.Substring(wordStart).Trim)
Else
Debug.Print(RichTextBox1.Text.Substring(wordStart, cursorAt - wordStart + wordEnd).Trim)
End If
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|