finding text the mouse clicked

How can you find the text a mouse clicks on a RichTextBox ...

For ex.  "Hello and this is just a random statement" (return character here)
"This is a statement after the above one"

If someone clicks on the first "statement" how could you find that?
LVL 10
ThaSmartUnoAsked:
Who is Participating?
 
KhollADConnect With a Mentor Commented:
Try the trick below :

Private Sub RichTextBox1_Click()
Dim pos1 As Integer
Dim pos2 As Integer
Dim txt As String
Dim txtlen As Integer

    ' Find the beginning of the word.
    pos1 = RichTextBox1.SelStart
    txt = RichTextBox1.Text
    If pos1 > 1 Then
        Do While Mid$(txt, pos1, 1) <> " "
            pos1 = pos1 - 1
            If pos1 <= 1 Then Exit Do
        Loop
        If pos1 > 1 Then pos1 = pos1 + 1
    End If
   
    ' Find the end of the word.
    pos2 = pos1
    txtlen = Len(txt)
    Do While Mid$(txt, pos2, 1) <> " "
        pos2 = pos2 + 1
        If pos2 > txtlen Then Exit Do
    Loop
    If pos2 <= txtlen Then pos2 = pos2 - 1
   
    txt = Mid$(txt, pos1, pos2 - pos1 + 1)
    MsgBox txt
End Sub
0
 
ThaSmartUnoAuthor Commented:
Thanks
0
 
ThaSmartUnoAuthor Commented:
grr ... just one question ... in my example ... if you clicked the first statement ... it would be

"statement
This" (in the msgbox)

do you know how to just get statement?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ThaSmartUnoAuthor Commented:
if u need points ill throw in 50 pts
0
 
KhollADCommented:
Sorry, and Thanks for the points, try this new code :

Private Sub RichTextBox1_Click()
Dim pos1 As Integer
Dim pos2 As Integer
Dim pos3 As Integer
Dim txt As String

   
    pos1 = RichTextBox1.SelStart
    txt = RichTextBox1.Text
   
    'Find the end of the word.
    pos2 = RichTextBox1.Find(Chr(13), pos1)
    pos3 = RichTextBox1.Find(" ", pos1)
   
    If pos2 = -1 And pos3 = -1 Then
        pos2 = Len(txt)
    End If
    If pos2 < pos3 Then
        If pos2 = -1 Then
            pos2 = Len(txt)
        End If
    ElseIf pos3 < pos2 Then
        If pos3 = -1 Then
            pos2 = Len(txt)
        Else
            pos2 = pos3
        End If
    End If
   
    ' Find the beginning of the word.
    If pos1 > 1 Then
        Do While Mid$(txt, pos1, 1) <> " " And Asc(Mid$(txt, pos1, 1)) <> 13
            pos1 = pos1 - 1
            If pos1 <= 1 Then Exit Do
        Loop
        If pos1 > 1 Then pos1 = pos1 + 1
    End If
   
    If pos1 = 0 Then
        pos1 = 1
    End If
    txt = Mid$(txt, pos1, pos2 - pos1 + 1)
    MsgBox txt
End Sub


-- tell me if works fine to you.
0
 
Erick37Commented:


Option Explicit

Const EM_CHARFROMPOS = 215
Const EM_LINEFROMCHAR = &HC9
Const EM_LINELENGTH = &HC1
Const EM_GETLINE = &HC4

Private Sub RichTextBox1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    Dim pt As POINTAPI
    Dim sBuffer As String
    Dim lChar As Long, lLine As Long, lLen As Long
    pt.x = x / Screen.TwipsPerPixelX
    pt.y = y / Screen.TwipsPerPixelY
    With RichTextBox1
    lChar = SendMessage(.hWnd, EM_CHARFROMPOS, 0, pt)
    Debug.Print "Character "; lChar
    lLine = SendMessage(.hWnd, EM_LINEFROMCHAR, lChar, 0)
    Debug.Print "Line "; lLine
    lLen = SendMessage(.hWnd, EM_LINELENGTH, lChar, 0)
    Debug.Print "Line length "; lLen
    sBuffer = Space(lLen + 1)
    Debug.Print SendMessage(.hWnd, EM_GETLINE, lLine, ByVal sBuffer)
    Debug.Print StrConv(sBuffer, vbUnicode)
    End With
End Sub
0
 
ThaSmartUnoAuthor Commented:
hey Erick whats POINTAPI? ...

KhollAD  it works ... I just want to see if Ericks code will work ... if it will I would like that  better since its a smaller code
0
 
KhollADCommented:
PointApi:

Private Type POINTAPI
   X as Long
   Y as Long
End Type
0
All Courses

From novice to tech pro — start learning today.