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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KhollADCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.